ASP.NET 中的缓存控制标头

Cache-Control Headers in ASP.NET(ASP.NET 中的缓存控制标头)
本文介绍了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. 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 中的缓存控制标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)