我正在尋找方法來禁用 整個 ASP.Net MVC 網站 的瀏覽器緩存
我發現以下方法,
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.Cache.SetNoStore();
此外元標記方法 (它不會為我工作,因為一些 MVC 操作發送部分 html/json 通過 ajax,無頭,元標記)
<meta. http-equiv="PRAGMA" content="NO-CACHE">
但我正在尋找禁用整個網站的瀏覽器緩存的簡單方法。
解決方法 1:
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 都路由-因此假定您可以只彈出那里后面的代碼中。
解決方法 2:
創建一個從 IActionFilter 繼承的類。
public class NoCache : 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();
}
}
