萧山中兴建设有限公司网站,电子项目外包网站,wordpress新数据库,专业网站建设定制咨询区 Palani#xff1a;我在寻找一个方法禁用某个 ASP.NET MVC 网站的所有浏览器缓存#xff0c;我发现了如下方法。Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.Cache.SetNoStore();而且我也发现了可以在 html 上追加一个 meta 标记。… 咨询区 Palani我在寻找一个方法禁用某个 ASP.NET MVC 网站的所有浏览器缓存我发现了如下方法。
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.Cache.SetNoStore();而且我也发现了可以在 html 上追加一个 meta 标记。
meta http-equivPRAGMA contentNO-CACHE但这种方式不适合我因为我的网站中会有一些 ajax 请求自然就无法携带meta了。请问我如何在全局作用域下实现这么一个禁用浏览器缓存的功能回答区 JKG可以自定义一个继承 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] 标记即可比如下面的 Controller。
[NoCache]
[HandleError]
public class AccountController : Controller
{[NoCache][Authorize]public ActionResult ChangePassword(){return View();}
}NidhinSPradeep你可以在 Global.asax 下的 Application_BeginRequest 方法中实现此功能。protected void Application_BeginRequest(){Response.Cache.SetCacheability(HttpCacheability.NoCache);Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));Response.Cache.SetNoStore();}JKG你可以使用 Asp.NET 自带的 OutputCache 特性。
[System.Web.Mvc.OutputCache(NoStore true, Duration 0, VaryByParam *)]直接使用 OutputCache 特性的话会让这些代码零散在项目各处更好的做好应该是封装到一个 Controller 中然后让需要的 Controller 继承此 Controller 即可比如下面的代码。
[System.Web.Mvc.OutputCache(NoStore true, Duration 0, VaryByParam *)]
public class NoCacheController : Controller
{
}public class HomeController : NoCacheController
{
}点评区 全局禁用浏览器的Cache这需求有点奇葩哈不过本篇也学习到了如何全局性的配置有收获。