ASP.NET MVC how to disable the automatic caching option?

How to disable automatic browser caching from asp.Net mvc application?

Because I have a problem with caching as it caches all links. But sometimes it is automatically redirected to the DEFAULT INDEX PAGE page which stores its caching, and then all the time when I click on this link, it redirects me to the DEFAULT INDEX PAGE page.

So, does anyone know how to manually disable caching from ASP.NET MVC 4?

+69
caching asp.net-mvc-4
Oct 18 '12 at 6:11
source share
6 answers

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

Disable for all actions in the controller

 [OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] // will be applied to all actions in MyController, unless those actions override with their own decoration public 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 the 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 to the default MVC project 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 the OutputCacheAttribute application to be listed above for each action that will disable server and browser caching. You can still override this cache by adding OutputCacheAttribute to specific actions and controllers.

+132
Oct 18 '12 at 6:22
source share

There is no dot in HackedByChinese. It accepted a server cache with a client cache. OutputCacheAttribute manages the server cache (IIS http.sys cache), and not the browser (client) cache.

I give you a very small part of my code base. 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); } } 

Using:

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

Use it wisely, as it really disables the entire client cache. The only cache that is not disabled is the browser back cache. But there seems to be no way around this. Perhaps only using javascript to detect it and force page or page refresh.

+28
Oct 18 '12 at 21:35
source share

We can set the cache profile in the Web.config file, rather than set the cache values โ€‹โ€‹individually on the pages to avoid redundant code. We can reference the profile using the CacheProfile property of the OutputCache attribute. This cache profile will apply to all pages unless this page / method overrides these parameters.

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

And if you want to disconnect caching from your specific action or controller, you can override the configuration cache settings by decorating this specific action method, as shown below:

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

Hope this is clear and helpful to you.

+12
02 Sep '14 at 11:20
source share

If you want to prevent browser caching, you can use this code from ShareFunction

 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(); base.OnResultExecuting(filterContext); } 
+9
Sep 01 '14 at 21:09
source share

To solve on the page Install this on the layout page:

 <meta http-equiv="Cache-Control" content="no-cache"> <meta http-equiv="Pragma" content="no-cache"> <meta http-equiv="Expires" content="0"> 
+5
Dec 07 '15 at 10:47
source share

To make my answer visible to everyone, I move my comment to answer this question.

 <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"> <meta http-equiv="Pragma" content="no-cache"> <meta http-equiv="Expires" content="0"> 

This will work in all browsers (IE, Firefox and Chrome). Glad to hear that my answer worked for you @ Joseph Katzman

0
Apr 26 '19 at 8:57
source share



All Articles