简体   繁体   English

ASP.NET中的缓存控制标头

[英]Cache-Control Headers in ASP.NET

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. 我正在尝试为Web应用程序设置缓存控制标头(看起来我能够做到这一点),但我得到的是我认为标题响应中的奇数条目。 My implementation is as follows: 我的实现如下:

    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
-----
  1. Why does the "Cache-Control" property show up twice? 为什么“Cache-Control”属性会出现两次?
  2. Do I need both "Cache-Control" and the "Expires" properties? 我是否需要“Cache-Control”和“Expires”属性?
  3. Is "Page_Load" the best place to put this code? “Page_Load”是放置此代码的最佳位置吗?

Thanks! 谢谢!

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是否已设置为自动添加标头。

I don't see Cache-control appearing twice. 我没有看到Cache-control出现两次。 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. 请求中的那个可能是因为您在浏览器中按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. 我不知道你想用max-age实现什么。 The value is way too high since you converted the DateTime incorrectly to a TimeSpan. 由于您将DateTime错误地转换为TimeSpan,因此该值太高。 Why don't you just use TimeSpan.FromMinutes instead? 为什么不直接使用TimeSpan.FromMinutes?

Page load is okay. 页面加载没问题。 I usually mess around with HTTP headers there myself. 我自己经常乱用HTTP标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM