[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 583
  • Last Modified:

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.Cache.SetNoStore()
    System.Web.HttpContext.Current.Response.AddHeader("Pragma", "no-cache")%>

Open in new window

0
sanagarwl
Asked:
sanagarwl
  • 2
1 Solution
 
DhaestCommented:
Disable Page Caching
Source: http://coercedcode.blogspot.com/2007/08/another-off-topic-post-but-that-is-ok.html

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)

        {

            page.Response.Cache.SetExpires(DateTime.Now.AddDays(-30));

            page.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            page.Response.Cache.SetNoServerCaching();

            page.Response.Cache.SetNoStore();

        }

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

page.Response.Cache.SetNoStore();

That seems to make Firefox happy.
0
 
DhaestCommented:
Disabling Browser Caching In C# and ASP.NET
http://www.352media.com/rantingandraving/rave.aspx?raveid=325&id=212

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
HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();


}
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now