如何阻止chrome从WebApi缓存REST响应? [英] How to stop chrome from caching REST response from WebApi?

查看:375
本文介绍了如何阻止chrome从WebApi缓存REST响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET WebApi并使用以下代码来停止所有内容的缓存:

I am using ASP.NET WebApi and have the following code to stop caching in everything:

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):

The result headers look like this (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

我在阅读了有关之后添加了no-store错误(如何阻止缓存中的chrome )。

I added the "no-store" after reading about the bug (How to stop chrome from caching).

然而,无论我做什么,当我做一些导航我离开这个页面的东西,然后使用后退按钮时,chrome总是从缓存中加载:

However, no matter what I do, when I do something that navigates me away from this page, and then use the "back" button, chrome always loads from cache:

Request Method:GET
Status Code:200 OK (from cache)

有谁知道为什么会这样?我已经确认服务器永远不会被这个请求命中。

Does anyone have any idea why this is happening? I have confirmed that the server is never hit for this request.

推荐答案

答案是Chrome不喜欢Expires:Mon ,1月01日0001 00:00:00格林威治标准时间(基本上是一个假日期)。

The answer is that Chrome does not like "Expires:Mon, 01 Jan 0001 00:00:00 GMT" (a fake date, basically).

我将我的日期更改为他们在Google API中使用的日期,并且它工作:

I changed my date to be what they use in their Google API, and it worked:

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

因此,对于遇到此问题的其他人,请确定你将过期日期设置为这个任意日期!

So, for anyone else who comes across this problem, make sure you set your Expires date to this arbitrary date!

这篇关于如何阻止chrome从WebApi缓存REST响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆