Microsoft IIS 7: Enable Client Side Browser Caching for static Content in Web.config

To optimize the performance of a web site that is hosted in Microsoft’s Internet Information Server 7 it is reasonable to enable client side browser caching for static content (like for example image files, CSS files or JavaScript files) that probably won’t change in the near feature. You can find more information and best practices about utilizing browser caching here.

Client side caching could be enabled by putting a Web.config file with the following content in the directory that contains the files that should be cached:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="10.00:00:00" />
    </staticContent>
  </system.webServer>
</configuration>

Using the above example the files will be cached for 10 days in the browser. Detailed information about the <clientCache> element can be found here.

To disable the cache settings in a subdirectory, just create a new Web.config file in that folder and override the cache settings:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Cache-Control" value="no-cache" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

You can also use the <location> element to define the cache settings for a specific file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <location path="path/to/the/file">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="10.00:00:00" />
      </staticContent>
    </system.webServer>
  </location>
</configuration>