ASP.net: Prevent Caching of a Page


/ Published in: ASP
Save to your folder(s)

Code snippet prevents caching of the page by setting http header values and a LAST MODIFIED header to prevent google from caching too long. This should go into the page load event.


Copy this code and paste it in your HTML
  1. //prevent browsers from caching the page.
  2. Response.Cache.SetCacheability(HttpCacheability.NoCache);
  3.  
  4. #region Google cache fix (prevents google from cachine the page too long)
  5. System.IO.FileInfo currentInfo = new System.IO.FileInfo(Request.PhysicalApplicationPath);
  6. System.IO.FileInfo currentDirInfo = new System.IO.FileInfo(Request.PhysicalPath);
  7.  
  8. DateTime modifiedTimeToUse;
  9. if (currentInfo.LastWriteTime > currentDirInfo.LastWriteTime)
  10. modifiedTimeToUse = currentInfo.LastWriteTime;
  11. else
  12. modifiedTimeToUse = currentDirInfo.LastWriteTime;
  13.  
  14. Response.AppendHeader("Last-Modified", modifiedTimeToUse.ToString("ddd, dd MMM yyyy hh:mm:ss GMT"));
  15. #endregion

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.