30 May 2012

Disabling Browser Caching In VB and ASP.NET


The majority of advice on how to disable browser caching seems to revolve around how to make it work in IE.  However, in Firefox the fixes don't always seem to work.  Below are some notes and sample code on how to make it work for both browsers:

The SetNoStore() seems to be a necessity for FireFox 
The HttpCacheability.NoCache has an issue in IE (not sure about FireFox) when you are on a secure (https) page and are trying to send a file by setting the headers. It causes the file download to not work properly. 
SetValidUntilExpires(), SetRevalidation() – unsure if this is a requirement at this time. Was added because it was still giving problems in FireFox before SetNoStore() was found. 
SetExpires() I tried to set this to 5 days in the past and I still got the 2 second window.

Public Shared Sub DisablePageCaching()

 'Used for disabling page caching 
 HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1))
 HttpContext.Current.Response.Cache.SetValidUntilExpires(False)
 HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches)
 HttpContext.Current.Response.Cache.SetAllowResponseInBrowserHistory(False)
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache) HttpContext.Current.Response.Cache.SetNoStore() End Sub

No comments:

Post a Comment