缓存控制:无存储,必须重新validation未发送到IIS7 + ASP.NET MVC中的客户端浏览器
我正在尝试确保某个页面永远不会被缓存,并且在用户单击后退按钮时从不显示。 这个非常高评价的答案(目前1068票)表示使用 :
Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); Response.AppendHeader("Pragma", "no-cache"); Response.AppendHeader("Expires", "0");
但是在IIS7 / ASP.NET MVC中,当我发送这些头时,客户端会看到这些响应头:
Cache-control: private, s-maxage=0 // that's not what I set them to Pragma: no-cache Expires: 0
缓存控制头怎么了? IIS7或ASP.NET本机的内容是否会覆盖它? 我检查了我的解决方案,我没有覆盖此标头的代码。
当我添加Response.Headers.Remove("Cache-Control");
首先,它没有区别:
Response.Headers.Remove("Cache-Control"); Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); Response.AppendHeader("Pragma", "no-cache"); Response.AppendHeader("Expires", "0");
当我添加[OutputCache]
属性时:
[OutputCache(Location = OutputCacheLocation.None)] public ActionResult DoSomething() { Response.Headers.Remove("Cache-Control"); Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); Response.AppendHeader("Pragma", "no-cache"); Response.AppendHeader("Expires", "0"); var model = DoSomething(); return View(model); }
然后客户端响应标头更改为:
Cache-control: no-cache Pragma: no-cache Expires: 0
哪个更接近,但仍然不是我想发送的标题。 这些标题被覆盖的位置在哪里?如何阻止它?
编辑:我已经检查过,错误的标题被发送到Chrome,FF,IE和Safari,所以它看起来是一个服务器问题,而不是浏览器相关的问题。
通过反复试验,我发现在ASP.NET MVC中为IIS7正确设置标头的一种方法是:
Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.AppendCacheExtension("no-store, must-revalidate"); Response.AppendHeader("Pragma", "no-cache"); Response.AppendHeader("Expires", "0");
第一行将Cache-control
为no-cache
,第二行添加其他属性no-store, must-revalidate
。
这可能不是唯一的方法,但如果更简单的Response.AppendHeader("Cache-control", "no-cache, no-store, must-revalidate");
,它确实提供了另一种方法Response.AppendHeader("Cache-control", "no-cache, no-store, must-revalidate");
失败。
可能由此解决的其他相关IIS7缓存控制问题是:
- 有些东西迫使响应具有缓存控制:IIS7中的私有
- IIS7:缓存设置不起作用……为什么?
- IIS7 + ASP.NET MVC客户端缓存标头不起作用
- 为aspx页面设置缓存控制
- 缓存控制:无存储,必须重新validation未发送到IIS7 + ASP.NET MVC中的客户端浏览器