no cache

Posted on 2008-11-12
Last Modified: 2012-05-05
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

Question by:sanagarwl
    LVL 53

    Expert Comment

    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.
    LVL 53

    Accepted Solution

    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


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
    This video teaches viewers about errors in exception handling.
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now