禁用整个ASP.NET网站的浏览器caching

我正在寻找一种方法来禁用整个ASP.NET MVC网站的浏览器caching

我发现了以下方法:

Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache); Response.Cache.SetNoStore(); 

也是一个元标记方法(它不会为我工作,因为一些MVC操作通过Ajax发送部分HTML / JSON,没有头,元标记)。

 <meta http-equiv="PRAGMA" content="NO-CACHE"> 

但我正在寻找一种简单的方法来禁用整个网站的浏览器caching。

 HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1)); HttpContext.Current.Response.Cache.SetValidUntilExpires(false); HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches); HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); HttpContext.Current.Response.Cache.SetNoStore(); 

所有的请求首先通过default.aspx路由 – 所以假设你可以在后面的代码中popup。

创build一个从IActionFilterinheritance的类。

 public class NoCacheAttribute : 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(); base.OnResultExecuting(filterContext); } } 

然后把属性放在需要的位置

 [NoCache] [HandleError] public class AccountController : Controller { [NoCache] [Authorize] public ActionResult ChangePassword() { return View(); } } 

而不是滚动你自己,只需使用为你提供的。

如前所述,不要禁用caching的一切。 例如,在ASP.NET MVC中大量使用的jQuery脚本应该被caching。 其实理想的情况是你应该为这些使用CDN ,但我的观点是应该caching一些内容。

我发现在这里最好的作品,而不是把[OutputCache]洒在任何地方都是使用一个类:

 [System.Web.Mvc.OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")] public class NoCacheController : Controller { } 

你想要禁用caching的所有控制器,然后从这个控制器inheritance。

如果您需要覆盖NoCacheController类中的默认值,只需在您的操作方法中指定caching设置,并且Action方法上的设置将优先。

 [HttpGet] [OutputCache(NoStore = true, Duration = 60, VaryByParam = "*")] public ViewResult Index() { ... } 

您可能希望对控制器(即HTML页面)呈现的所有页面禁用浏览器caching,但为脚本,样式表和图像等资源保留caching 。 如果你使用MVC4 +绑定和缩小,你会想要保持脚本和样式表的默认caching持续时间(很长的持续时间,因为caching取决于对唯一URL的改变而不是基于时间)。

在MVC4 +中,要禁用所有控制器上的浏览器caching,但保留它不是由控制器提供的任何内容,请将其添加到FilterConfig.RegisterGlobalFilters

 filters.Add(new DisableCache()); 

定义DisableCache如下:

 class DisableCache : ActionFilterAttribute { public override void OnResultExecuting(ResultExecutingContext filterContext) { filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache); } } 

我知道这个答案不是100%相关的问题,但它可能有助于某人。

如果你想禁用整个ASP.NET MVC网站的浏览器caching,但是你只想做TEMPORARILY,那么最好在你的浏览器中禁用caching。

以下是Chrome中的屏幕截图

我实现了所有以前的答案,仍然有一个视图不能正常工作。

事实certificate,我遇到的问题的名称被命名为“最近”。 显然这使IE浏览器困惑。

在将视图名称(在控制器中)更改为其他名称(我select“Recent5”)之后,上述解决scheme开始工作。

您可以在Global.asax文件中的代码下面尝试。

 protected void Application_BeginRequest() { Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1)); Response.Cache.SetNoStore(); } 

UI

 <%@ OutPutCache Location="None"%> <% Response.Buffer = true; Response.Expires = -1; Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1); Response.CacheControl = "no-cache"; %> 

背景

 Context.Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Expires = -1; Response.Cache.SetNoStore();