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.效果

HttpContext.Cache属性的更多相关文章

  1. 第十七篇:使用窗口的cache属性加速SOUI的渲染

    内容渲染速度是决定一个UI成败的关键.无论UI做得多华丽,没有速度都没有意义. 在MFC,WTL等开发框架下,每个控件都是一个窗口,窗口只需要画前景,背景.因为窗口之间的内容不需要做混合,一个子窗口的 ...

  2. HttpContext.Cache 详解

    提到HttpContext.Cache必然会想到Application,他们有什么共性和不同点呢,我们一一道来 相同点: 1.两者都是使用键值对来存储对象 2.两者都是应用程序同生命周期(在cache ...

  3. HttpContext.Cache和Application的区别

    原文:HttpContext.Cache和Application的区别 (转载)   应用程序级的Cache和Application用户会话级的Session application的缺点是在读取时最 ...

  4. ASP.NET中HttpContext.Cache的使用

    -------------------------------键 --值-----依赖-----过期时间-------------------------------绝对过期------------- ...

  5. Asp.Net framework 类库 自带的缓存 HttpRuntime.Cache HttpContext.Cache

    两个Cache 在.NET运用中经常用到缓存(Cache)对象.有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是应用程序 ...

  6. 关于Ajax 的 cache 属性 (Day_34)

    最近做项目,在某些页面显示,ajax刷新总是拿不到新内容,时常需要清除缓存,才能到达想要的效果. 经过再次查看文档,最后加了一行属性:cache:false 即可解决问题 我们先看下文档的说明: 可以 ...

  7. 缓存 HttpContext.Current.Cache和HttpRuntime.Cache的区别

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

  8. HttpContext.Current.Cache 和HttpRuntime.Cache的区别

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

  9. HttpContext.Current.Cache 和 HttpRuntime.Cache 区别

    原文地址:http://blog.csdn.net/avon520/article/details/4872704 .NET中Cache有两种调用方式:HttpContext.Current.Cach ...

随机推荐

  1. 2-kvm创建快照以及网卡绑定

    kvm创建快照以及网卡绑定 创建node1 查看node1 进入到kvm的配置文件里 将rhcs文件复制一份取名为node1.xml 通过这个命令随机生成一个uuid 然后就进入node1.xml里修 ...

  2. 怎么知道Fragment属于哪个Activity呢?

    如果程序是一条线运行的,Fragment 中 getActivity() 是获取的上一个打开或者执行的Activity中的值.   多个Activity也是如此,就看顺序是怎么执行的,getActiv ...

  3. centos mysql开启远程访问

    登录MySQL:  mysql -u root -p db; 如需修改密码,第一次:  mysqladmin -u root password NEWPASSWORD 已设置过:  mysqladmi ...

  4. find your present (感叹一下位运算的神奇)

    find your present (2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. vs2013 打开vs2010 找不到此项目类型所基于的应用程序 MVC2 升级 MVC5 不能加载Web项目

    Upgrading an ASP.NET MVC 2 Project to ASP.NET MVC 3 Tools Update ASP.NET MVC 3 can be installed side ...

  6. redis 学习笔记(7)-cluster 客户端(jedis)代码示例

    上节学习了cluster的搭建及redis-cli终端下如何操作,但是更常用的场景是在程序代码里对cluster读写,这需要redis-client对cluster模式的支持,目前spring-dat ...

  7. [LeetCode] Majority Element II 求众数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  8. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  9. [LeetCode] Anagrams 错位词

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

  10. <img>标签链接地址失效如何自动显示默认图片

    <img src="errurl" onerror="this.src='default.jpg'">