aut*_*ton 12 rest caching http browser-cache asp.net-web-api
我正在使用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;
}
Run Code Online (Sandbox Code Playgroud)
结果标题看起来像这样(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
Run Code Online (Sandbox Code Playgroud)
在阅读了有关错误(如何阻止缓存中的chrome)之后,我添加了"no-store" .
但是,无论我做什么,当我做一些导航我远离此页面的东西,然后使用"后退"按钮时,chrome总是从缓存中加载:
Request Method:GET
Status Code:200 OK (from cache)
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么会这样?我已经确认服务器永远不会被这个请求命中.
答案是Chrome不喜欢"Expires:Mon,01 Jan 01 00 00:00:00 GMT"(基本上是假日期).
我将我的日期更改为他们在Google API中使用的日期,并且有效:
Cache-Control:no-store, must-revalidate, no-cache, max-age=0
Content-Length:1897
Content-Type:application/json; charset=utf-8
Date:Fri, 19 Jul 2013 20:51:49 GMT
Expires:Mon, 01 Jan 1990 00:00:00 GMT
Pragma:no-cache
Server:Microsoft-IIS/8.0
Run Code Online (Sandbox Code Playgroud)
因此,对于遇到此问题的任何其他人,请确保将过期日期设置为此任意日期!
归档时间: |
|
查看次数: |
14744 次 |
最近记录: |