gpt4 book ai didi

asp.net - 禁用整个 ASP.NET 网站的浏览器缓存

转载 作者:行者123 更新时间:2023-12-03 04:10:39 32 4
gpt4 key购买 nike

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

我找到了以下方法:

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

还有一个元标记方法(它对我不起作用,因为一些 MVC 操作通过 Ajax 发送部分 HTML/JSON,没有 head、元标记)。

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

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

最佳答案

创建一个继承自 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();
}
}

关于asp.net - 禁用整个 ASP.NET 网站的浏览器缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1160105/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com