3

Using asp.net in visual studios and IIS7 when i get a host.

I have a folder full of icons that will rarely change and is used on every page. Is there a way i can set a certain directory to expire something like once every 2 or so hours so i can decrease the amount of incoming request to the server?

1 Answers1

14

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.

Gabriel McAdams
  • 56,921
  • 12
  • 61
  • 77
  • How do i say check back in 4 hours or of midnight of every day? I'd like holiday themes and such. –  Jan 30 '10 at 20:18
  • 2
    If you want to have the ability to change images at any time, there are 2 options. 1) Use the above method, and change the URL when you change images, and change them back at the end of the holiday. 2) Set the expires date smaller. – Gabriel McAdams Jan 30 '10 at 20:22
  • This looks like a fixed date. Cant i set it relative or use code to change it? –  Jan 30 '10 at 21:48
  • I added how to make it relative to my answer – Gabriel McAdams Jan 30 '10 at 22:33
  • Just as an info: this setting seems to have no effect on the built-in Development Server (of VS 2010 SP1 at least). If in doubt why it doesn't work, check with the IIS. –  Apr 19 '11 at 16:32
  • any idea why it's not setting the cache for my flash banner & my favicon ? – danfromisrael May 12 '11 at 21:56