Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache-Control Headers in ASP.NET

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.


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.

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?

Page load is okay. I usually mess around with HTTP headers there myself.