HttpContext基于HttpApplication的处理管道,由于HttpContext对象贯穿整个处理过程,所以,可以从HttpApplication处理管道的前端将状态数据传递到管道的后端,完成状态的传递任务做个小demo

1.控制器:

  public class TestController : Controller{string key = "data";public ActionResult Index(){return View();}/// <summary>/// 定时器获取缓存数据/// </summary>/// <returns></returns>
        [HttpPost]public JsonResult GetData(){string data = Convert.ToString(HttpContext.Cache.Get(this.key));if (!string.IsNullOrEmpty(data)){return this.Json(new { success = true, data = data });}else{return this.Json(new { success = false, time = DateTime.Now.ToString("ss"), data = data });}}/// <summary>/// 打开输入缓存值界面/// </summary>/// <returns></returns>
        [HttpGet]public ActionResult CreateCacheData(){return View();}/// <summary>/// 将数据插入缓存/// </summary>/// <param name="value"></param>/// <returns></returns>
        [HttpPost]public void InsertCache(string value){HttpContext.Cache.Insert(this.key, value);        }}

2.视图

Index.cshtml:

@{ViewBag.Title = "Index";Layout = null;}
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.timers-1.2.js"></script><button id="btnStart">开始定时器</button>
<script>$(function (){//定时器开始
        $("#btnStart").bind("click", function () {$("body").everyTime("3s", "timer", function () {$.ajax({type: 'post',url: '/Test/GetData',success: function (r) {if (r.success) {console.log("获取到数据,json字符串为" + JSON.stringify(r.data));}else {console.log("(" +r.time + ")秒没有获取到数据");}}});})});})
</script>

jquery.timers-1.2.js 是定时器jquery插件定时器插件下载

CreateCacheData.cshtml:

@{ViewBag.Title = "CreateCacheData";
}
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<input id="txtData"/>
<button id="btnSave" >插入服务器缓存
</button><script>$(function () {$("#btnSave").click(function (){var getDataValue = $("#txtData").val();$.post("/Test/InsertCache", {value:getDataValue}, function () {alert("缓存插入成功");});})});
</script>

3.效果

转载于:https://www.cnblogs.com/CallmeYhz/p/5979345.html

HttpContext.Cache属性相关推荐

  1. HttpContext.Current.Cache vs. HttpRuntime.Cache

    .NET中Cache有两种调用方式:HttpContext.Current.Cache 和 HttpRuntime.Cache,这两种方式有什么区别呢?我们先看MSDN上的解释:       Http ...

  2. HttpContext.Current.Cache和HttpRuntime.Cache的区别,以及System.Runtime.Caching

    先看MSDN上的解释:       HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象.       HttpRuntime.Cache:获取当前应用程序的C ...

  3. Asp.net中的Cache--HttpRuntim.Cache 和 HttpContext.Current.Cache

    在ASP.NET中有两个类都提供缓存支持, 一个是HttpRuntime类的Cache属性, 另一个是HttpContext类的Cache属性. 通过查看这两个属性的类型可以发现其实这两个属性都是Sy ...

  4. .NET Core 中有等价的 HttpContext.Response.Cache 吗?

    咨询区 jackmusick: 我想禁掉浏览器缓存,这样我的client端每次都能看到server端的最新内容,在 asp.net 时代可以这么写. public class NoCacheAttri ...

  5. System.Web.Caching.Cache类 缓存 各种缓存依赖

    原文:System.Web.Caching.Cache类 缓存 各种缓存依赖 Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.C ...

  6. .net System.Web.Caching.Cache缓存类使用详解(转载)

    转自:http://www.cnblogs.com/virusswb/articles/1681561.html net System.Web.Caching.Cache缓存类使用详解 System. ...

  7. 细说 ASP.NET Cache 及其高级用法

    阅读目录 开始 Cache的基本用途 Cache的定义 Cache常见用法 Cache类的特点 缓存项的过期时间 缓存项的依赖关系 - 依赖其它缓存项 缓存项的依赖关系 - 文件依赖 缓存项的移除优先 ...

  8. System.Web.Caching.Cache

            System.Web.Caching 命名空间提供用于缓存服务器上常用数据的类.这包括 Cache 类,该类是一个使您可以存储任意数据对象(如哈希表和数据集)的词典.它还为这些对象提供 ...

  9. Android SystemProperties设置/取得系统属性的用法总结

    2019独角兽企业重金招聘Python工程师标准>>> 1.Android SystemProperties简介 介绍了设置属性需要的权限,已经设置权限的方法. Systemprop ...

最新文章

  1. java生成xsd_java 生成XSD
  2. 《代码大全》阅读笔记02
  3. 【java开发系列】—— 自定义注解
  4. 为什么阿里巴巴禁止把SimpleDateFormat定义为static类型的?
  5. IP协议详解之子网寻址、子网掩码、构造超网
  6. 每天一道LeetCode-----给定字符串s和字符数组words,在s中找到words出现的位置,words内部字符串顺序无要求
  7. 2018年工业机器人销量排位_2020年全球工业机器人现状与竞争情况 中国为全球最大供应国...
  8. [Luogu1891]疯狂LCM[辗转相减法]
  9. es6删除数组某一项_「JavaScript 从入门到精通」10.数组
  10. Ubuntu 18.04安装Eclipse教程
  11. Knockout.js学习笔记----绑定
  12. 一文搞懂鱼眼相机模型
  13. 【调剂】上海理工大学软件工程接收调剂研究生
  14. 纯手工打war包方法
  15. 【JDK8 新特性 6】收集Stream流中的结果
  16. 2021-10-26第八章思考题
  17. 2020年第一季度某区房地产市场运行情况分析
  18. Python批量下载apk
  19. Java GUI 实现win10记事本几乎所有的功能
  20. Multisim电路分析仿真-一阶动态电路三种响应曲线

热门文章

  1. Replica small data to PostgreSQL from Oracle's Big table
  2. kafka入门:简介、使用场景、设计原理、主要配置及集群搭建(转)
  3. Linux01-BASH的while流程控制41
  4. SQL2005中的XXproperty() 函数归纳
  5. 输出和为n的所有的连续自然数序列
  6. 组信箱共享及挂载介绍
  7. Changes at Different Environment?
  8. south eastern china at a scope of 100km
  9. identification for completing the census
  10. 一个特别棒的远程在线学习方法的分享!!!妈妈再也不怕我的网课录屏啦学不进去啦!!!