【发布时间】:2014-04-22 01:01:01
【问题描述】:
我试图确保某个页面永远不会被缓存,并且当用户单击后退按钮时永远不会显示。 This very highly rated answer (currently 1068 upvotes) says to use:
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,所以它看起来是服务器问题而不是浏览器相关问题。
【问题讨论】:
-
我无法在新的 MVC3 或 MVC4 应用程序中复制此问题。您能检查一下您在 IIS 中的设置(HTTP 响应标头 和输出缓存)吗?
-
在 IIS7 中,我没有为输出缓存(服务器级别或站点级别)配置任何设置,并且只配置了一个响应标头(X-Powered-By)
标签: c# asp.net-mvc iis-7 http-headers cache-control