Posted By

jink on 05/04/09


Tagged

aspnet cache prevent


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

benrudolph


ASP.net: Prevent Caching of a Page


 / Published in: ASP
 

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.

  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  

You need to login to post a comment.