HttpContext the HttpApplication-based processing pipeline, because the HttpContext object runs through the entire process , the state data can be passed from the front end of the HttpApplication processing pipeline to the back end of the pipeline. Complete the status of the delivery task to do a little demo
1. Controller:
Public classTestcontroller:controller {stringKey ="Data"; PublicActionResult Index () {returnView (); } /// <summary> ///timer gets cached data/// </summary> /// <returns></returns>[HttpPost] PublicJsonresult GetData () {stringdata = 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> ///Open the input cache value interface/// </summary> /// <returns></returns>[HttpGet] PublicActionResult Createcachedata () {returnView (); } /// <summary> ///inserting data into the cache/// </summary> /// <param name= "value" ></param> /// <returns></returns>[HttpPost] Public voidInsertcache (stringvalue) {HttpContext.Cache.Insert ( This. Key, value); } }
2. View
Index.cshtml:
@{viewbag.title = "Index"; Layout = null;}<Scriptsrc= "~/scripts/jquery-1.8.2.min.js"></Script><Scriptsrc= "~/scripts/jquery.timers-1.2.js"></Script><ButtonID= "Btnstart">Start timer</Button><Script> $(function () { //Timer Start $("#btnStart"). Bind ("Click", function () { $("Body"). EveryTime ("3s", "Timer", function() {$.ajax ({type:'Post', URL:'/test/getdata', Success:function(r) {if(r.success) {Console.log ("gets the data to the JSON string as" +json.stringify (r.data)); } Else{Console.log ("(" +R.time+ ") seconds did not get to the data"); } } }); }) }); })</Script>
Jquery.timers-1.2.js is a timer jquery plugin
Timer plugin Download
Createcachedata.cshtml:
@{viewbag.title = "Createcachedata";}<Scriptsrc= "~/scripts/jquery-1.8.2.min.js"></Script><inputID= "Txtdata"/><ButtonID= "Btnsave" >Insert Server Cache</Button><Script> $(function () { $("#btnSave"). Click (function () { varGetdatavalue= $("#txtData"). Val (); $.post ("/test/insertcache", {value:getdatavalue},function() {alert ("Cache Insert succeeded"); }); }) });</Script>
3. Effects
,
Httpcontext.cache Property