且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET MVC和IE缓存 - 操纵响应报头无效

更新时间:2023-02-23 18:10:13

我觉得你应该只使用

HttpContext.Current.Response.Cache.SetMaxAge (new TimeSpan (0));

HttpContext.Current.Response.Headers.Set ("Cache-Control", "private, max-age=0");

设置最大年龄= 0 这意味着没有更多的缓存重新验证(见的这里)。如果你将与来自数据散列的一些自定义的校验头另行的ETag 设置,从previous请求的ETag将被发送到服务器。服务器能够要么返回数据,或在情况下的数据是完全和以前一样,它可以返回空的身体和的HTTPStatus code.NotModified 作为状态code。在这种情况下Web浏览器将从本地浏览器缓存中获取数据。

to set max-age=0 which means nothing more as the cache re-validating (see here). If you would be set additionally ETag in the header with some your custom checksum of hash from the data, the ETag from the previous request will be sent to the server. The server are able either to return the data or, in case that the data are exactly the same as before, it can return empty body and HttpStatusCode.NotModified as the status code. In the case the web browser will get the data from the local browser cache.

我建议你使用缓存控制:私人迫使两个重要的事情:1)关闭缓存的代理,这有时非常积极的缓存设置数据2)它会允许的数据的高速缓存,而不是允许与另一个用户缓存的共享。它可以解决隐私问题,因为您返回到一个用户的数据都将不可被其他用户读取。顺便code HttpContext.Current.Response.Cache.SetMaxAge(新的TimeSpan(0))设置缓存控制:私人的,最大年龄= 0 默认情况下的HTTP标头。如果你想使用缓存控制:公开您可以使用 SetCacheability(HttpCacheability.Public); 来覆盖行为或使用 Headers.Set 而不是 Cache.SetMaxAge

I recommend you to use Cache-Control: private which force two important things: 1) switch off caching the data on the proxy, which has sometimes very aggressive caching settings 2) it will allows the caching of the the data, but not permit sharing of the cache with another users. It can solve privacy problems because the data which you return to one user could be not allowed to read by another users. By the way the code HttpContext.Current.Response.Cache.SetMaxAge (new TimeSpan (0)) set Cache-Control: private, max-age=0 in the HTTP header by default. If you do want to use Cache-Control: public you can use SetCacheability (HttpCacheability.Public); to overwrite the behavior or use Headers.Set instead of Cache.SetMaxAge.

如果您有兴趣学习HTTP协议的更多的缓存选项,我建议您阅读缓存教程

If you have interest to study more caching options of HTTP protocol I would recommend you to read the caching tutorial.

更新时间::我决定写一些更多的信息,清楚我的立场。对应于的信息来自于***即便如此旧的Web浏览器,如马赛克2.7 的版本,Netscape 2.0和Internet Explorer 3.0支持三月1996年,$ p $ HTTP的p-标准/ 1.1 RFC 2068中描述,所以我想(而不是测试),旧的浏览器都支持最大年龄= 0 HTTP标头。以任何方式的Netscape 2.06和Internet Explorer 4.0明确支持HTTP 1.1。

UPDATED: I decide to write some more information to clear my position. Corresponds to the information from the Wikipedia even so old web browsers like Mosaic 2.7, Netscape 2.0 and Internet Explorer 3.0 supports March 1996, pre-standard of HTTP/1.1 described in RFC 2068. So I suppose (but not test it) that the old web browsers support max-age=0 HTTP header. In any way Netscape 2.06 and Internet Explorer 4.0 definitively supports HTTP 1.1.

所以,你应该先问问你:你使用的HTML标准?你还在用HTML 2.0,而不是更晚HTML 3.2刊登在1997年1月?我想你至少使用刊登在1997年12月所以,如果你至少在HTML 4.0构建应用程序,您的网站可以在Web客户导向,支持HTTP 1.1,而忽略HTML 4.0(不支持)的Web客户端这不支持HTTP 1.1。

So you should ask you first: which HTML standards you use? Do you still use HTML 2.0 instead of more late HTML 3.2 published in January 1997? I suppose you use at least HTML 4.0 published in December 1997. So if you build your application at least in HTML 4.0, your site can be oriented on the web clients which supports HTTP 1.1 and ignore (don't support) the web clients which don't support HTTP 1.1.

现在关于其他缓存控制头为私人,最大年龄= 0。包括头在我看来是的纯偏执。由于我有一些问题,缓存我自己也试过,包括不同的其它头,但仔细阅读RFC2616的14.9节后来经过我只用缓存控制:私人,最大年龄= 0。

Now about other "Cache-Control" headers as "private, max-age=0". Including of the headers is in my opinion is pure paranoia. As I have some caching problem myself I tried also to include different other headers, but later after reading carefully the section 14.9 of RFC2616 I use only "Cache-Control: private, max-age=0".

唯一的缓存控制,它可以另外讨论的标头是必须重新验证关于这一点我之前提到的14.9.4节描述。这里是报价:

The only "Cache-Control" header which can be additionally discussed is "must-revalidate" described on the section 14.9.4 which I referenced before. Here is the quote:

的必备revalidate指令是必要的支持可靠
  对于某些协议功能的操作。在任何情况下的
  HTTP / 1.1缓存必须服从必备revalidate指令;尤其是,
  如果缓存不能以任何理由到原服务器,它必须
  产生一个504(网关超时)响应。

The must-revalidate directive is necessary to support reliable operation for certain protocol features. In all circumstances an HTTP/1.1 cache MUST obey the must-revalidate directive; in particular, if the cache cannot reach the origin server for any reason, it MUST generate a 504 (Gateway Timeout) response.

服务器应该发送的必revalidate指令当且仅当
  未能重新验证的实体可能会导致一个请求
  不正确的操作,诸如默默地未执行金融
  交易。收件人必须没有采取任何自动操作的
  违反该指令,不得将自动提供
  实体的未经验证的副本,如果再验证失败。

Servers SHOULD send the must-revalidate directive if and only if failure to revalidate a request on the entity could result in incorrect operation, such as a silently unexecuted financial transaction. Recipients MUST NOT take any automated action that violates this directive, and MUST NOT automatically provide an unvalidated copy of the entity if revalidation fails.

虽然这是
  不推荐使用,严重的连接下操作的用户代理
  限制可能违反这项指令,但如果是这样,必须明确
  警告说,一个未经验证的响应已提供给用户。该
  一定要提供每个未经验证的访问警告,并应
  需要明确的用户确认。

Although this is not recommended, user agents operating under severe connectivity constraints MAY violate this directive but, if so, MUST explicitly warn the user that an unvalidated response has been provided. The warning MUST be provided on each unvalidated access, and SHOULD require explicit user confirmation.

有时候,如果我有互联网连接我看到了空白页以网关超时消息的问题。它来自必须重新验证指令的用法。我不认为网关超时消息,真正帮助用户。

Sometime if I have problem with Internet connection I see the empty page with "Gateway Timeout" message. It come from the the usage of "must-revalidate" directive. I don't think that "Gateway Timeout" message really help the user.

于是人,preFER如何,如果他听到在电话会议上向他的老板忙的信号开始自我毁灭的过程中,应另外使用必须重新验证指令中的缓存控制头。我建议其他人只是用缓存控制:私人,最大年龄= 0,仅此而已。

So the persons, how prefer to start self-destructive procedure if he hears "Busy" signal on the call to his boss, should additionally use "must-revalidate" directive in the "Cache-Control" header. Other persons I recommend just use "Cache-Control: private, max-age=0" and nothing more.