C# ASP.NET 中的缓存控制标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/945898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cache-Control Headers in ASP.NET
提问by
I am trying to set the cache-control headers for a web application (and it appears that I'm able to do it), but I am getting what I think are odd entries in the header responses. My implementation is as follows:
我正在尝试为 Web 应用程序设置缓存控制标头(似乎我能够做到),但是我在标头响应中得到了我认为是奇怪的条目。我的实现如下:
protected override void OnLoad(EventArgs e)
{
// Set Cacheability...
DateTime dt = DateTime.Now.AddMinutes(30);
Response.Cache.SetExpires(dt);
Response.Cache.SetMaxAge(new TimeSpan(dt.ToFileTime()));
// Complete OnLoad...
base.OnLoad(e);
}
And this is what the header responses show:
这就是标题响应显示的内容:
-----
GET /Pages/Login.aspx HTTP/1.1
Host: localhost:1974
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
X-lori-time-1: 1244048076221
Cache-Control: max-age=0
HTTP/1.x 200 OK
Server: ASP.NET Development Server/8.0.0.0
Date: Wed, 03 Jun 2009 16:54:36 GMT
X-AspNet-Version: 2.0.50727
Content-Encoding: gzip
Cache-Control: private, max-age=31536000
Expires: Wed, 03 Jun 2009 17:24:36 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 6385
Connection: Close
-----
- Why does the "Cache-Control" property show up twice?
- Do I need both "Cache-Control" and the "Expires" properties?
- Is "Page_Load" the best place to put this code?
- 为什么“Cache-Control”属性出现两次?
- 我是否需要“Cache-Control”和“Expires”属性?
- “Page_Load”是放置此代码的最佳位置吗?
Thanks!
谢谢!
回答by chris166
I don't see Cache-control appearing twice. One is in the request, one is in the response. The one in the request is probably because you hit Shift+F5 in the browser or something similar.
我没有看到 Cache-control 出现两次。一个在请求中,一个在响应中。请求中的那个可能是因为您在浏览器中按了 Shift+F5 或类似的东西。
To your second question: that depends on what you want to achieve with the cache headers.
你的第二个问题:这取决于你想用缓存头实现什么。
I don't know what you wanted to achieve with the max-age. The value is way too high since you converted the DateTime incorrectly to a TimeSpan. Why don't you just use TimeSpan.FromMinutes instead?
我不知道你想用 max-age 达到什么目的。该值太高了,因为您将 DateTime 错误地转换为 TimeSpan。为什么不直接使用 TimeSpan.FromMinutes 呢?
Page load is okay. I usually mess around with HTTP headers there myself.
页面加载没问题。我自己通常会在那里处理 HTTP 标头。
回答by BigBlondeViking
You might also want to add this line if you are setting the max age that far out :
如果您设置的最大年龄太远,您可能还想添加这一行:
// Summary:
// Sets Cache-Control: public to specify that the response is cacheable
// by clients and shared (proxy) caches.
Response.Cache.SetCacheability(HttpCacheability.Public);
I do a lot of response header manip with documents and images from a file handler that processes requests for files the are saved in the DB.
我对文件处理程序中的文档和图像进行了大量响应头操作,该处理程序处理对保存在数据库中的文件的请求。
Depending on your goal you can really force the browsers the cache almost all of you page for days locally ( if thats what u want/need ).
根据您的目标,您可以真正强制浏览器在本地缓存几乎所有页面数天(如果那是您想要/需要的)。
edit:
编辑:
I also think you might be setting the max age wrong...
我也认为您可能设置的最大年龄错误...
Response.Cache.SetMaxAge(new TimeSpan(dt.Ticks - DateTime.Now.Ticks ));
this line set is to 30 min cache time on the local browser [max-age=1800]
此行设置为本地浏览器上的 30 分钟缓存时间 [max-age=1800]
As for the 2x Cache Control lines... you might want to check to see if IIS has been set to add the header automatically.
至于 2x 缓存控制行...您可能需要检查 IIS 是否已设置为自动添加标头。