no cache

Given below is the code I am using in a .net application to prevent browser from caching -- I'm not sure why but this is not working. I also used the script -- Response.Cache.SetNoStore() in the page load event of the masterpage and that too is not working.

any help would be much appreciated
<%  System.Web.HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache")
System.Web.HttpContext.Current.Response.Expires = 0
    System.Web.HttpContext.Current.Response.AddHeader("Pragma", "no-cache")%>

Open in new window

Who is Participating?
DhaestConnect With a Mentor Commented:
Disabling Browser Caching In C# 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 static void DisablePageCaching()

//Used for disabling page caching

Disable Page Caching

There are many pages on the out there with information about how to disable page caching. I could not find any which were correct. A lot of the information was partially correct, but it didn't work for the newer Firefox browsers.

I tested all this by making a test page where I display a timestamp and move to another page, then I hit the back button to see if the timestamp is updated or if it is just showing me the old one.

Previously with just the first three lines of this function which was my old disable page cache function it worked fine with Internet Explorer, but it was not working with Firefox. I do not remember if it used to work for Firefox, but I know it wasn't working for me now.

Now I think I have figured out and it seems to be working for both IE and Firefox. So without further delay here is the code for all you aspiring programmers out there.

        public static void DisablePageCaching(Page page)







The secret ingredient, the key thing which was missing from my previous implementation is this line:


That seems to make Firefox happy.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.