I have an asp.net web site and I would like the client browser to cache the HTML output of aspx pages once it gets them.
From reading around I found out that IIS7 does not support this out-of-the-box, so I added the following code to the OnLoad event of my main master page:
protected override void OnLoad(EventArgs e) {
DateTime dt = DateTime.Now.AddDays(10);
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetExpires(dt);
Response.Cache.SetMaxAge(new TimeSpan(dt.Ticks - DateTime.Now.Ticks));
base.OnLoad(e);
}
With this I still get the following in the response header:
HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Type: text/html; charset=utf-8 Content-Encoding: gzip Expires: -1 Vary: Accept-Encoding Date: Fri, 05 Jul 2013 14:25:03 GMT Content-Length: 10201
Maybe I did something a long time ago to override this, but I can’t, for the life of me remember if and what I did. Not finding any info on this when searching Google makes me think that maybe cache-control for aspx pages is not a good idea… but I need this in order for cloudfront (a CDN) to allow whole site delivery.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Somewhere in the IIS pipeline (I have yet to find out where), no cache headers were set. In my master page, before setting the cache headers I wanted to use, I added the following code Response.ClearHeaders(); This might not be a good solution for everyone, since you might have some headers you need in the response already, but in my case it did the trick.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0