Add Expire headers in Asp.net

8.3k Views Asked by At

I am using YSlow to grade the performance of my application and I got an D grade for the Add Expires Header aspect.

I am using IIS express, so I found this code online and added it into the web.config file but it doesnt work; the grade is still D.

I did it in the web.config file. Any idea how to improve the grade of "Add Expires Header" ?

    <system.webServer>
   <staticContent>

<clientCache cacheControlMaxAge="30.00:00:00" cacheControlMode="UseMaxAge"/>

</staticContent>
2

There are 2 best solutions below

0
MadDev On

Please try the below:

  1. IIS -> Expand Sites -> Select your site -> Click 'HTTP Response Headers' -> Under 'Actions' on the right hand side, Click 'Set Common Headers' -> Tick 'Expire Web Content checkbox' -> Select 'After' radio button and then set the Days value to 7.

    https://technet.microsoft.com/en-us/library/cc770661(v=ws.10).aspx

  2. IIS -> Expand Sites -> Select your site -> Click 'Compression' -> Tick 'Enable dynamic content compression' and 'Enable static content compression'.

    https://www.iis.net/configreference/system.webserver/httpcompression

1
Anish On

The configuration has to be made in the root of the application. The general practice is 365 days. This config only applies to static file requests.

<configuration>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
    </staticContent>
  </system.webServer>
</configuration>

Ensure that the config shows up in "%programfiles(x86)%\IIS Express\appcmd" list config "YOUR SITE" -section:system.webServer/staticcontent

For dynamic file requests, you would have configure output caching .