9

Like the question says, I wanted to know if it's possible to turn off caching on all controllers and actions for my entire site. Thanks!

enamrik
  • 2,292
  • 2
  • 27
  • 42

4 Answers4

15

Create a Global Action Filter and override OnResultExecuting():

public class DisableCache : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
        filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        filterContext.HttpContext.Response.Cache.SetNoStore();
    }
}

And then register this in your global.asax, like so:

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new DisableCache());
    }

In summation, what this does is create a Global Action Filter so that implicitly this will be applied to all Controllers and all Actions.

  • Thanks for your reply. How about scripts, images and css? would the global action filter affect those too? In other words does an action filter only get called for requests to controllers/actions or on every request? Like @AdamTuliper mentioned, I wouldn't want to cache those – enamrik Feb 23 '12 at 02:30
  • @enamrik Hmmm... after testing it out it looks like images and css still cache with this global action filter. I'd have to research how to prevent this. –  Feb 23 '12 at 03:16
  • Nevermind, apparently that's the very definition of an actionfilter that they only run on requests to actions – enamrik Feb 23 '12 at 03:16
  • @enamrik Right, exactly. Maybe this is something that needs to be handled on the client-side..? –  Feb 23 '12 at 03:17
  • Well I'm not really concerned with scripts, images and css being cached. I kind of don't have the time to dig deeper into the matter but if the time comes when I don't want to cache my scripts images and css (sensitive logos?) I'll ask another question, :-). Thank! – enamrik Feb 23 '12 at 05:14
  • Update for MVC4: This approach works nicely with bundling and minification. HTML pages aren't cached, but resources such as bundled scripts and CSS are still cached. – Edward Brey Mar 28 '13 at 02:24
  • In MVC 5 and other MVCs the `RegisterGlobalFilters` could be in `App_Start\FilterConfig.cs` – Csaba Toth Mar 13 '20 at 21:50
5

You should add this method to your Global.asax.cs file

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            Response.AddHeader("Pragma", "no-cache"); // HTTP 1.0.
            Response.AddHeader("Expires", "0"); // Proxies.
        }

This disables cache on every request (images, html, js etc.).

lyubeto
  • 125
  • 3
  • 11
1

In web.config you can add additional headers to go out with every response

<configuration>
    <system.webServer>
        <httpProtocol>
          <customHeaders>
            <add name="Cache-control" value="no-cache"/>
          </customHeaders>
        </httpProtocol>
    </system.webServer>
</configuration>
Peter Morris
  • 20,174
  • 9
  • 81
  • 146
1

Yes, depending on the approach you take. I like applying the actions to a base controller (hence my reply there). You could implement the filter at the link below and implement it as a global filter as well (registered in your global.asax.cs)

Disable browser cache for entire ASP.NET website

Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71