ASP.NET MVC how to disable automatic caching option? ASP.NET MVC how to disable automatic caching option? asp.net asp.net

ASP.NET MVC how to disable automatic caching option?


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

Disable for all actions in a controller

[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] // will be applied to all actions in MyController, unless those actions override with their own decorationpublic class MyController : Controller{  // ... }

Disable for a specific action:

public class MyController : Controller{    [OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] // will disable caching for Index only    public ActionResult Index()    {       return View();    }} 

If you want to apply a default caching strategy to all actions in all controllers, you can add a global action filter by editing your global.asax.cs and looking for the RegisterGlobalFilters method. This method is added in the default MVC application project template.

public static void RegisterGlobalFilters(GlobalFilterCollection filters){    filters.Add(new OutputCacheAttribute                    {                        VaryByParam = "*",                        Duration = 0,                        NoStore = true,                    });    // the rest of your global filters here}

This will cause it to apply the OutputCacheAttribute specified above to every action, which will disable server and browser caching. You should still be able to override this no-cache by adding OutputCacheAttribute to specific actions and controllers.


HackedByChinese is missing the point. He mistook server cache with client cache. OutputCacheAttribute controls server cache (IIS http.sys cache), not browsers (clients) cache.

I give you a very small part of my codebase. Use it wisely.

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]public sealed class NoCacheAttribute : FilterAttribute, IResultFilter{    public void OnResultExecuting(ResultExecutingContext filterContext)    {    }    public void OnResultExecuted(ResultExecutedContext filterContext)    {        var cache = filterContext.HttpContext.Response.Cache;        cache.SetCacheability(HttpCacheability.NoCache);        cache.SetRevalidation(HttpCacheRevalidation.ProxyCaches);        cache.SetExpires(DateTime.Now.AddYears(-5));        cache.AppendCacheExtension("private");        cache.AppendCacheExtension("no-cache=Set-Cookie");        cache.SetProxyMaxAge(TimeSpan.Zero);    }}

Usage:

/// will be applied to all actions in MyController[NoCache]public class MyController : Controller{    // ... }

Use it wisely as it really disables all client cache. The only cache not disabled is the "back button" browser cache. But is seems there is really no way to get around it. Maybe only by using javascript to detect it and force page or page zones refresh.


We can set cache profile in the Web.config file instead of setting cache values individually in pages to avoid redundant code. We can refer the profile by using the CacheProfile property of the OutputCache attribute. This cache profile will be applied to all pages unless the page/method overrides these settings.

<system.web>  <caching>    <outputCacheSettings>      <outputCacheProfiles>        <add name="CacheProfile" duration="60" varyByParam="*" />      </outputCacheProfiles>    </outputCacheSettings>  </caching></system.web>

And if you want to disable the caching from your particular action or controller, you can override the config cache settings by decorating that specific action method like shown below:

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]public ActionResult NoCachingRequired(){    return PartialView("abcd");}

Hope this is clear and is useful to you.