Asking browsers to cache our images (ASP.NET/IIS) Asking browsers to cache our images (ASP.NET/IIS) asp.net asp.net

Asking browsers to cache our images (ASP.NET/IIS)


I found the answer to my question elsewhere on this site. Woot! (Not sure why it didn't appear when I first posted this, but never mind, I got there in the end.)

For those interested, the answer was this (as posted by Gabriel McAdams):


You do that in IIS. If you are using IIS 7, you can add the header in your web.config. It’s in the system.webServer section.

<staticContent>    <clientCache httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" cacheControlMode="UseExpires" /></staticContent>

This will cause all static content to have an expires HTTP header set to the year 2020. Static content means anything that isn’t served through the ASP.NET engine such as images, script files and styles sheets.

Or to use a relative expiration, use this:

<staticContent>    <clientCache cacheControlMaxAge ="2.00:00:00" cacheControlMode="UseMaxAge" /></staticContent>

This will cause all static content to have an expires HTTP header set to 2 days.


You will have to add Expires Header to your Static content including images, html, js, css files. You can easily add the expires header in your web.config’s system.webServer section using IIS7:

<staticContent>    <clientCache httpExpires="Mon, 1 May 2020 05:00:00 GMT" cacheControlMode="UseExpires" /></staticContent>


What you typically want is to cache all assets (css,js and images), the html file will have çache busting links so that you can still update the images.

In order for this to work, the html files cannot be part of staticContent. adding a new handler for html files prevents them from being permanently cached.

    <staticContent>        <clientCache httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" cacheControlMode="UseExpires" />    </staticContent>    <handlers>      <add        name="HtmlHandler"        path="*.html"        verb="*"        type="System.Web.Handlers"        preCondition="integratedMode"      />    </handlers>