Links

 

ASP.NET: How to Prevent Browser Page Caching and Successfully Expire Content

Page and Cache expiration on IIS is sometimes not completely obvious. One common problem is that certain browsers (in our case IE), seem to be more prone than others, to cache pages indefinitely, especially when they are configured with their default installation settings. When we were working on an AJAX JSON webservice page recently, IE kept caching the old responses, and the UI was behaving erratically. With this code snippet placed in the OnInit event of the page, it solved all of the caching problems we encountered.
Response.AddHeader("Cache-Control", "no-cache"); Response.ExpiresAbsolute = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0)); Response.Expires = 0; Response.Cache.SetNoStore(); Response.AddHeader("Pragma", "no-cache");
All of these settings combined seemed to do the trick.
Submit a comment, suggestion, or additional information about this snippet
If you login or register, you'll be able to post a comment or provide feedback about this snippet.
Comments
There is a good caching article at codeproject. http://www.codeproject.com/KB/aspnet/ExploringCaching.aspx
Contact Us | Terms of Use