Httpcontext.cache Property

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.