Setting Far Future Expires Header In Code - ASP.NET

两盒软妹~` 提交于 2019-12-04 07:36:32

If you're using IIS 7, the easiest way to do it would be to write an HttpModule that runs for static files in Integrated mode, and set the Expires and Cache-Control headers from there.

Update:

Your HttpModule should work, although I normally also call:

context.Response.Cache.SetMaxAge(TimeSpan.FromDays(365.));

Update 2:

With IIS 6, you would have to programmatically modify the metabase. It's possible, although it requires elevated permissions.

The only other option would be to write an ISAPI module in C++.

Even though YSLOW made the recommendation, you may also benefit from reading Mr. Atwood's article: YSlow: Yahoo's Problems Are Not Your Problems.

From the article:

Add an Expires Header (Weight: 11)

This isn't bad advice, per se, but it can cause huge problems if you get it wrong. In Microsoft's IIS, for example, the Expires header is always turned off by default, probably for that very reason. By setting an Expires header on HTTP resources, you're telling the client to never check for new versions of that resource-- at least not until the expiration date on the Expires header. When I say never, I mean it -- the browser won't even ask for a new version; it'll just assume its cached version is good to go until the client clears the cache, or the cache reaches the expiration date. Yahoo notes that they change the filename of these resources when they need them refreshed.

So I guess one of the takeaways is: suppose you change the contents of a master css file, but you don't also rename the css file. If you take Yahoo's recommendation, your end user won't get the updated version of the file you edited until the expiration date of the header. Are you comfortable with such a scenario?

Good ways of improving performance include: gzip-compressing responses (not the easiest with IIS6), minifying static files (css, js), merging static files together (one big css, one big js), using sprites. The idea is to reduce the total number of HTTP requests and then to reduce the size of the responses.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!