Disabling browser caching for all browsers from ASP.NET Disabling browser caching for all browsers from ASP.NET asp.net asp.net

Disabling browser caching for all browsers from ASP.NET


This is what we use in ASP.NET:

// Stop Caching in IEResponse.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);// Stop Caching in FirefoxResponse.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-storePragma: no-cache


For what it's worth, I just had to handle this in my ASP.NET MVC 3 application. Here is the code block I used in the Global.asax file to handle this for all requests.

    protected void Application_BeginRequest()    {        //NOTE: Stopping IE from being a caching whore        HttpContext.Current.Response.Cache.SetAllowResponseInBrowserHistory(false);        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);        HttpContext.Current.Response.Cache.SetNoStore();        Response.Cache.SetExpires(DateTime.Now);        Response.Cache.SetValidUntilExpires(true);    }


I've tried various combinations and had them fail in FireFox. It has been a while so the answer above may work fine or I may have missed something.

What has always worked for me is to add the following to the head of each page, or the template (Master Page in .net).

<script language="javascript" type="text/javascript">    window.onbeforeunload = function () {           // This function does nothing.  It won't spawn a confirmation dialog           // But it will ensure that the page is not cached by the browser.    }  </script>

This has disabled all caching in all browsers for me without fail.