缓存控制头在ASP.NET [英] Cache-Control Headers in ASP.NET

查看:117
本文介绍了缓存控制头在ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置为Web应用程序缓存控制标头(和它看来,我能够做到这一点),但我得到什么,我认为是在头反应奇条目。我的实现如下:

 保护覆盖无效的OnLoad(EventArgs的发送)
    {
        //设置缓存能力...
        DateTime的DT = DateTime.Now.AddMinutes(30);
        Response.Cache.SetExpires(DT);
        Response.Cache.SetMaxAge(新的TimeSpan(dt.ToFileTime()));        //完整的OnLoad ...
        base.OnLoad(E);
    }

这是个什么头反应显示:

  -----
GET /Pages/Login.aspx HTTP / 1.1
主机:本地主机:1974
用户代理:Mozilla的/ 5.0(视窗; U; Windows NT的5.1; EN-US; RV:1.9.0.10)的Gecko / 2009042316火狐/ 3.0.10(.NET CLR 3.5.30729)
接受:text / html的,是application / xhtml + xml的,应用/ XML; Q = 0.9 * / *; Q = 0.8
接受语言:EN-US,连接; Q = 0.5
接受编码:gzip,紧缩
接收字符集:ISO-8859-1,UTF-8,Q = 0.7 *; Q = 0.7
保持活动:300
连接:保持活动
的X洛瑞时间-1:1244048076221
缓存控制:最大年龄= 0HTTP / 1.x的200 OK
服务器:ASP.NET开发服务器/ 8.0.0.0
日期:星期三,2009年16点54分36秒格林尼治标准​​时间06月03日
的X ASPNET-版本:2.0.50727
内容编码:gzip
缓存控制:私人,最大年龄= 31536000
到期日:星期三,2009年17点24分三十六秒GMT 06月03日
内容类型:text / html的;字符集= UTF-8
内容长度:6385
连接:关闭
-----


  1. 为什么缓存控制的属性出现两次?

  2. 请我既需要缓存控制和过期属性?

  3. 是的Page_Load把这个code的最佳地点?

谢谢!


解决方案

您可能也想加入这一行,如果你设置的最大年龄也差不了多少了:

  //摘要:
//设置缓存控制:公众对指定的响应缓存
//客户端和共享(代理)缓存。
Response.Cache.SetCacheability(HttpCacheability.Public);

我做了很多从处理的保存在DB文件请求的文件处理程序文档和图像响应头MANIP的。

根据你的目标,你真的可以强制浏览器缓存中,几乎所有你几天页面的局部(如果多数民众赞成什么ü想/需要)。

编辑:

我也想你可能会设置最大年龄错了...

  Response.Cache.SetMaxAge(新的TimeSpan(dt.Ticks  -  DateTime.Now.Ticks));

这行集是30分钟,缓存时间上的本地浏览器[最大年龄= 1800]

至于2倍高速缓存控制线......你可能要检查,看是否IIS已被设置为自动添加标题。

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:

    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?
  2. Do I need both "Cache-Control" and the "Expires" properties?
  3. Is "Page_Load" the best place to put this code?

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]

As for the 2x Cache Control lines... you might want to check to see if IIS has been set to add the header automatically.

这篇关于缓存控制头在ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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