52

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

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?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Marek
  • 10,307
  • 8
  • 70
  • 106

3 Answers3

79

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

Marek
  • 10,307
  • 8
  • 70
  • 106
  • how is this different than setting a far future expire date in IIS ?? – leora Mar 19 '11 at 02:54
  • 3
    not sure if there is any difference - feel free to check the differences in HTTP traffic :) The mentioned approach has an advantage that it is possible to alter the caching behavior without configuring IIS (e.g. on shared hosting) – Marek Mar 21 '11 at 10:27
  • What if, one of the image changes before the cache expiry date. How do we initial the 302 request for that image? – sameer Oct 28 '14 at 17:30
  • 1
    @sameer if it's a different image, use a different name, or use a cache-buster (querystring with date of file) – Tracker1 Feb 10 '15 at 17:17
  • Also, for anyone curious, when you hit refresh on the browser, it will do an if-modified-since request for each file... if you navigate, you'll see the cache response. – Tracker1 Feb 10 '15 at 17:32
  • i tried this but i have a issue [here](http://stackoverflow.com/questions/35704618/asp-mvc-expire-headers-not-set-for-static-contents) is my question, expire header is not set – Shaiju T Mar 12 '16 at 11:54
31

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.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • 2
    Shouldn't that be DateTime.UtcNow? If not, can you explain how the browser knows what the local time of the server is? (Update: actually http://stackoverflow.com/questions/4849744/should-i-use-datetime-now-or-datetime-utcnow-in-httpcookie-expires-and-httpcache answers this question) – Michiel Cornille May 10 '16 at 15:22
  • 1
    Good question. As you discovered, it doesn't matter. – Drew Noakes May 10 '16 at 17:15
2

Look at mini static content delivery project. :)

dariol
  • 1,959
  • 17
  • 26