Salient Solutions

wrasslin ones and nones for fun and profit - Sky Sanders' Blog
Get your own ranked flair here
posts - 92, comments - 108, trackbacks - 0

Preventing caching of content in ASP.Net Pages and other HttpHandlers

 

Sometimes I forget to squash caching on my JSON handlers and end up with odd runtime behavior, especially when using IE.

You can ensure that each request actually pulls data instead of hitting the cache by adding this to the top of a handler (.ashx) or in the page_load of a .aspx. Drop 'context.' for use in page_load.

 

context.Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Cache.SetNoStore();

 

This is what the head of my JSON handlers typically look like:

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ClearContent();
            context.Response.ClearHeaders();
            context.Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.Cache.SetNoStore();
            context.Response.ContentType = "text/json";

Technorati tags: , ,

Print | posted on Tuesday, March 23, 2010 2:59 AM | Filed Under [ CodeProject-Tip ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 1 and 5 and type the answer here:

Powered by: