1
votes

I have a web application which is written in ASP.NET 4. I'm using IIS7 and want to locally cache all my static content (js, css, images...). I've added this code to the web.config of the web application

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

Though it seems that each time I do a request for some image or css file, I first get 200 response and then 304 Not Modified response. Instead of the browser just using a local cache version of that static file.

For example this is a request and response for an image file, for second time I request it:


Request:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8

Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3

Accept-Encoding:gzip,deflate,sdch

Accept-Language:en-US,en;q=0.8

Cache-Control:max-age=0

Connection:keep-alive

Cookie:USER_ITEMS_SORTBY_COOKIEKEY=7; ASP.NET_SessionId=gi4yazmfarsdvzedx2ltpdyt;

Host:dev.y****e.co.il

If-Modified-Since:Wed, 11 Jan 2012 19:27:14 GMT

If-None-Match:"6bc4ec597d0cc1:0"

User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko)

Chrome/17.0.963.78 Safari/535.11

Response Headers

Accept-Ranges:bytes

Date:Sat, 10 Mar 2012 22:32:57 GMT

ETag:"6bc4ec597d0cc1:0"

Expires:Sun, 29 Mar 2020 00:00:00 GMT

Last-Modified:Wed, 11 Jan 2012 19:27:14 GMT

Server:Microsoft-IIS/7.0

X-Powered-By:ASP.NET


I also tried using the approach of adding directly to IIS, but still no go.

Thanks

1

1 Answers

1
votes

For specific folder

<configuration>
  <location path="images">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:00:15" />
      </staticContent>
    </system.webServer>
  </location>
</configuration>

For Specific file

<configuration>
    <location path="imagefile.jpg">
        <system.webServer>
            <staticContent>
                <clientCache cacheControlMode="DisableCache" />
            </staticContent>
        </system.webServer>
    </location>
</configuration>

Here location path means specify file/folder need implementation for your application...

cacheControlMaxAge - The default value is 1.00:00:00 (1 day).