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. javascript-模板方法模式-提示框归一化插件

    模板方法模式笔记   父类中定义一组算法操作骨架,而将一些实现步骤延迟到子类中,使得子类可以不改变父类的算法结构的同时可重新定义算法中某些实现步骤   实例:弹出框归一化插件 css样式 ;width ...

  2. 【MSSQL】MSSQL还原单mdf文件报1813错误

    序: MS SQL Server 2008 r2附加无ldf日志的mdf数据库时报1813错误.提示数据库被强制分离,无日志不能还原之类的话. 可能的原因: 原因系统正在执行定时作业,没有主要到多次强 ...

  3. XSS 防御方法总结

    1. XSS攻击原理 XSS原称为CSS(Cross-Site Scripting),因为和层叠样式表(Cascading Style Sheets)重名,所以改称为XSS(X一般有未知的含义,还有扩 ...

  4. Monthly Income Report – August 2016

    原文链接:https://marcoschwartz.com/monthly-income-report-august-2016/ Every month, I publish a report of ...

  5. Fragment之间传值

    Activity: String myArguments;    public String getarguments() {   return myArguments;  } public void ...

  6. heredoc技术

    Heredoc技术,在正规的PHP文档中和技术书籍中一般没有详细讲述,只是提到了这是一种Perl风格的字符串输出技术.但是现在的一些论坛程序,和部分文章系统,都巧妙的使用heredoc技术,来部分的实 ...

  7. [python]设计模式

    需要说明:java跟python在思维模式上并不一样,java利用接口以及多态可以实现很多抽象上的东西,而python不行,其实以下很多设计模式写法并不适用也没有必要,更多是为了对比和帮助理解这些设计 ...

  8. java 成长之路[轉載u]

    分享总结title: java 成长之路tags:grammar_cjkRuby: true 经验差异 1-3年 要求 建议 3-5年 建议 5年+ 经验差异 最近一年比较忙,经历了创业公司的倒闭.这 ...

  9. Unity中脚本的执行顺序总结(@WhiteTaken)

    (Editor)以上是Unity官方文档中的截图,脚本在被挂载到物体上,会启用Editor的方法Reset. (Initialization)当执行脚本开始,初始化的过程中,依次执行的是Awake-& ...

  10. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...