我正在尝试重现我在这里找到的以前版本的ASP.NET的东西.
基本上,我希望能够禁用缓存,以便我的客户端始终向服务器查找信息.我为此添加了一个HTML元标记,但是对于已经拥有此信息的客户端,我想尝试在后端处理缓存策略.
帖子提到这样做是为了将缓存策略设置为动作过滤器.
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); } }
但是,HttpContext似乎在ASP.NET Core中没有Response.Cache.有没有其他方法可以做到这一点?
谢谢!
您可以直接将相应的响应标头设置为所需的值:
public class NoCacheAttribute : ActionFilterAttribute { public override void OnResultExecuting(ResultExecutingContext filterContext) { filterContext.HttpContext.Response.Headers["Cache-Control"] = "no-cache, no-store, must-revalidate"; filterContext.HttpContext.Response.Headers["Expires"] = "-1"; filterContext.HttpContext.Response.Headers["Pragma"] = "no-cache"; base.OnResultExecuting(filterContext); } }