Saturday, February 11, 2012

Quick tip to fix Cache Control issue with IE 8

There are instances where you want the page cache to say get out of my way and let me go to the server. One such instance would be when you are trying to create a PDF document out of a page response.  It’s quite common to leave the HttpCacheability property alone or in some instances set the property to "Server" or “ServerAndNoCache”) to get optimum performance.


If the page is going to be extremely dynamic as the pdf generation case mentioned above, then you are left with no choice other than setting the  HttpCacheability to NoCache.  The general syntax is as follows

Response.Cache.SetCacheability(HttpCacheability.NoCache)

This way of setting the cache control header will not work properly for IE 8 (and some older browsers).  So the following old fashioned syntax is respected by all the browsers including the IE brothers.

Response.AddHeader("Cache-Control", " no-store, no-cache ");