How to control static content caching and cache expiry in ASP.NET web application using web.config

Posted: (EET/GMT+2)

 

If your ASP.NET web application contains static files, such as HTML files, CSS files, and so on, you might wish to control their caching. This is useful for performance reasons, but also allows you to control file caching in case the files change often.

In web.config, you can have the section "staticContent" under "system.webServer".

Here is an example that sets the caching age to at most five (5) seconds for all files in the StaticViews folder of the project:

<!-- set maximum age for static files for easier editing during development -->
<location path="StaticViews">
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:00:05" />
    </staticContent>
  </system.webServer>
</location>

Hope this helps!