我正在寻找一种方法来禁用整个ASP.NET MVC网站的浏览器缓存。
我发现了以下方法:
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.Cache.SetNoStore();还有一个元标记方法(它不适用于我,因为一些MVC操作通过Ajax发送部分HTML/JSON,而没有head,meta标记)。
<meta http-equiv="PRAGMA" content="NO-CACHE">但我正在寻找一个简单的方法,以禁用整个网站的浏览器缓存。
发布于 2009-07-21 16:00:59
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路由--所以假设您可以在后面弹出代码。
发布于 2009-11-10 01:28:50
创建一个从IActionFilter继承的类。
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();
}
}发布于 2011-04-05 01:57:07
与其使用自己的产品,不如简单地使用为你提供的东西。
如前所述,不要禁用所有的缓存。例如,应该缓存大量使用在jQuery MVC中的ASP.NET脚本。实际上,理想情况下,您应该对这些内容使用CDN,但我想说的是,应该缓存一些内容。
我发现这里最有效的方法是使用一个类,而不是到处撒OutputCache:
[System.Web.Mvc.OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public class NoCacheController : Controller
{
}您希望禁用缓存的所有控制器,然后从该控制器继承。
如果需要覆盖NoCacheController类中的默认值,只需在操作方法上指定缓存设置,操作方法上的设置将优先。
[HttpGet]
[OutputCache(NoStore = true, Duration = 60, VaryByParam = "*")]
public ViewResult Index()
{
...
}https://stackoverflow.com/questions/1160105
复制相似问题