缓存控制:无存储,必须重新验证未发送到IIS7 + ASP.NET MVC中的客户端浏览器

JK.*_*JK. 28 c# asp.net-mvc iis-7 cache-control http-headers

我试图确保某个页面永远不会被缓存,并且在用户单击后退按钮时从不显示. 这个非常高评价的答案(目前1068支票)说使用:

Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
Response.AppendHeader("Pragma", "no-cache");
Response.AppendHeader("Expires", "0");
Run Code Online (Sandbox Code Playgroud)

但是在IIS7/ASP.NET MVC中,当我发送这些头时,客户端会看到这些响应头:

Cache-control: private, s-maxage=0 // that's not what I set them to
Pragma: no-cache
Expires: 0
Run Code Online (Sandbox Code Playgroud)

缓存控制头怎么了?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");
Run Code Online (Sandbox Code Playgroud)

当我添加一个[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);
}
Run Code Online (Sandbox Code Playgroud)

然后客户端响应标头更改为:

Cache-control: no-cache
Pragma: no-cache
Expires: 0
Run Code Online (Sandbox Code Playgroud)

哪个更接近,但仍然不是我要发送的标题.这些标题在哪里被覆盖,我该如何阻止它?

编辑:我已经检查过,错误的标题被发送到Chrome,FF,IE和Safari,所以它看起来是一个服务器问题,而不是浏览器相关的问题.

JK.*_*JK. 44

通过反复试验,我发现在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");
Run Code Online (Sandbox Code Playgroud)

第一行设置Cache-controlno-cache,第二行添加其他属性no-store, must-revalidate.

这可能不是唯一的方法,但如果更直接的Response.AppendHeader("Cache-control", "no-cache, no-store, must-revalidate");失败,确实提供了另一种方法.

可能由此解决的其他相关IIS7缓存控制问题是:

  • 我喜欢MVC和IIS如何决定在这里和那里随机打破自己的API并引入无声失败. (3认同)