Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling browser caching for all browsers from ASP.NET

I'm after a definitive reference to what ASP.NET code is required to disabled browsers from caching the page. There are many ways to affect the HTTP headers and meta tags and I get the impression different settings are required to get different browsers to behave correctly. It would be really great to get a reference bit of code commented to indicate which works for all browsers and which is required for particular browser, including versions.

There is a huge amount of information about this issue there but I have yet to find a good reference that describes the benefits of each method and whether a particular technique has been superseded by a higher level API.

I'm particularly interested in ASP.NET 3.5 SP1 but it would be good to get answers for earlier version as well.

This blog entry Two Important Differences between Firefox and IE Caching describes some HTTP protocol behaviour differences.

The following sample code illustrates the kind of thing I am interested in

public abstract class NoCacheBasePage : System.Web.UI.Page {     protected override void OnInit(EventArgs e)     {         base.OnInit(e);          DisableClientCaching();     }      private void DisableClientCaching()     {         // Do any of these result in META tags e.g. <META HTTP-EQUIV="Expire" CONTENT="-1">         // HTTP Headers or both?          // Does this only work for IE?         Response.Cache.SetCacheability(HttpCacheability.NoCache);          // Is this required for FireFox? Would be good to do this without magic strings.         // Won't it overwrite the previous setting         Response.Headers.Add("Cache-Control", "no-cache, no-store");          // Why is it necessary to explicitly call SetExpires. Presume it is still better than calling         // Response.Headers.Add( directly         Response.Cache.SetExpires(DateTime.UtcNow.AddYears(-1));     } } 
like image 673
Martin Hollingsworth Avatar asked May 27 '09 05:05

Martin Hollingsworth


People also ask

How do I stop my browser from caching?

Here's how... When you're in Google Chrome, click on View, then select Developer, then Developer Tools. Alternatively, you can right click on a page in Chrome, then click Inspect. Click on the Network tab, then check the box to Disable cache.

How do I stop Chrome from caching localhost?

To activate it you have to go to: More Tools > Developer tools > Network "tab" then click on Disable cache.

What is the property that used to disable the cache storage in ASP.NET MVC?

You can use the OutputCacheAttribute to control server and/or browser caching for specific actions or all actions in a controller.


1 Answers

This is what we use in ASP.NET:

// Stop Caching in IE Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);  // Stop Caching in Firefox Response.Cache.SetNoStore(); 

It stops caching in Firefox and IE, but we haven't tried other browsers. The following response headers are added by these statements:

Cache-Control: no-cache, no-store Pragma: no-cache 
like image 93
HttpWatchSupport Avatar answered Oct 07 '22 15:10

HttpWatchSupport