我想确保某个页面永远不被缓存,并且当用户点击“返回”按钮时永远不会显示。 这个非常高评价的答案(目前有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,因此看起来是一个服务器问题而不是一个浏览器相关的问题。