Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

c# - 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. 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...