How to specify HTTP expiration header? (ASP.NET MVC+IIS)

asp.net MvcCachingIisHttp Headers

asp.net Mvc Problem Overview


I am already using output caching in my ASP.NET MVC application.

http://code.google.com/speed/page-speed/">Page speed tells me to specify HTTP cache expiration for css and images in the response header.

I know that the Response object contains some properties that control cache expiration. I know that these properties can be used to control HTTP caching for response that I am serving from my code:

Response.Expires
Response.ExpiresAbsolute
Response.CacheControl

or alternatively

Response.AddHeader("Expires", "Thu, 01 Dec 1994 16:00:00 GMT");

The question is how do I set the Expires header for resources that are served automatically, e.g. images, css and such?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

Found it:

I need to specify client cache for static content (in web.config).

<configuration>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlCustom="public" 
      cacheControlMaxAge="12:00:00" cacheControlMode="UseMaxAge" />
    </staticContent>
   </system.webServer>
</configuration>

from http://www.iis.net/ConfigReference/system.webServer/staticContent/clientCache

Solution 2 - asp.net Mvc

If you want to do it from code for a resource that you're returning (ie. not a static file being served from IIS), you're better off using Response.Cache:

Response.Cache.SetExpires(DateTime.Now.AddYears(1));
Response.Cache.SetCacheability(HttpCacheability.Public);

I know that's not exactly what you're asking for, but I found this question via Google and figure others might like this answer as it's related to the APIs you show in the original question text.

Solution 3 - asp.net Mvc

Look at mini static content delivery project. :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarekView Question on Stackoverflow
Solution 1 - asp.net MvcMarekView Answer on Stackoverflow
Solution 2 - asp.net MvcDrew NoakesView Answer on Stackoverflow
Solution 3 - asp.net MvcdariolView Answer on Stackoverflow