Wednesday, November 19, 2008

Disable / Prevent ASP.NET Caching

While we create an image/PDF on the fly, sometimes we will find when you request the second time, the file displayed is the cached file. The following code will disable Caching.

Response.Cache.SetNoServerCaching();
Response.Cache.SetCacheability(System.Web.HttpCacheability.Private);
Response.Cache.SetExpires(DateTime.MinValue);

In one stage, I tried "Response.Cache.SetNoStore();". It actually caused Content-Type not properly recognized. See the following article from Microsoft's website:

http://support.microsoft.com/default.aspx?scid=kb;en-us;812935

http://support.microsoft.com/?kbid=323308

Also, System.Web.HttpCacheability.Private is better than System.Web.HttpCacheability.NoCache, because NoCache means it's not saved in your disk as temporary file and would not be promote to be opened by Adobe Reader.

No comments: