我正在使用ASP.NET WebApi,并有以下代码来停止缓存:
public override System.Threading.Tasks.Task<HttpResponseMessage> ExecuteAsync(System.Web.Http.Controllers.HttpControllerContext controllerContext, System.Threading.CancellationToken cancellationToken)
{
System.Threading.Tasks.Task<HttpResponseMessage> task = base.ExecuteAsync(controllerContext, cancellationToken);
task.GetAwaiter().OnCompleted(() =>
{
task.Result.Headers.CacheControl = new CacheControlHeaderValue()
{
NoCache = true,
NoStore = true,
MaxAge = new TimeSpan(0),
MustRevalidate = true
};
task.Result.Headers.Pragma.Add(new NameValueHeaderValue("no-cache"));
task.Result.Content.Headers.Expires = DateTimeOffset.MinValue;
});
return task;
}
结果头看起来像这样(在Chrome中):
Cache-Control:no-store, must-revalidate, no-cache, max-age=0
Content-Length:1891
Content-Type:application/json; charset=utf-8
Date:Fri, 19 Jul 2013 20:40:23 GMT
Expires:Mon, 01 Jan 0001 00:00:00 GMT
Pragma:no-cache
Server:Microsoft-IIS/8.0
在阅读了关于这个 bug 的文章(如何停止 Chrome 缓存)后,我添加了 "no-store"。
然而,无论我做什么,当我从这个页面导航到其他页面,然后使用 "返回" 按钮时,Chrome 总是从缓存中加载:
Request Method:GET
Status Code:200 OK (from cache)
有人知道为什么会发生这种情况吗?我已确认该请求从未到达服务器。