How to set cache control for static content RRS feed

  • Question

  • User1553997479 posted

    I would like to set a maxage value for entities in my images directory, but I can find no way to do so. The IIS 7.0 Beta help indicates there should be a UI element for HTTP Headers, but there is none. None of the release documentation mentions this element.

    I tried using appcmd as well, but it didn't seem to have any effect.

    Currently the Last-Modified header is returned, so subsequent requests respond with 304 (Not Modified), but I would like the browser to just use it's local cache most of the time.


    Thursday, January 25, 2007 4:04 PM

All replies

  • User511787461 posted

    This property is not exposed in the UI - you can set it using appcmd - eg to set max-age to 1hr for all static files under /images on default-web-site, run the following

    \Windows\system32\inetsrv\appcmd.exe set config "Default Web Site/images" -section:system.webServer/staticContent -clientCache.cacheControlMode:UseMaxAge

    \Windows\system32\inetsrv\appcmd.exe set config "Default Web Site/images" -section:system.webServer/staticContent -clientCache.cacheControlMaxAge:"01:00:00"

    Friday, January 26, 2007 4:38 PM
  • User1488698909 posted
    Is it possible to cache static content with IIS 5 or 6?
    Friday, November 23, 2007 8:44 AM
  • User1759600568 posted

    here is a further research on the subject: IIS7: How to set cache control for static content?

    Hope this helps

    Wednesday, February 27, 2008 2:40 PM
  • User-1757254373 posted

    Is there a way to disable static caching on a site level? Or set the caching period for the entire site and not each folder?

    Saturday, July 12, 2008 12:23 PM
  • User511787461 posted

    Are you talking about server-side caching or client-side caching?  The posts above already talk about client-side caching, for server-side caching, you can do the following

    appcmd set config "My Site" -section:system.webServer/caching -enabled:false


    Wednesday, July 16, 2008 1:42 PM
  • User-1757254373 posted

    that's it, thanks

    Wednesday, July 16, 2008 5:25 PM
  • User1017348458 posted

    Hello Anilr,

    I am totally frustrated at this caching deal.

    I was originally using IIS 5.1 on Windows XP.  To turn the Cashe ISAPI applications

    check box off from the command line prompt, I would type in

    cscript.exe adsutil.vbs set w3svc/1/root/myWebApp/cacheisapi 0

    This would effectively turn off the IIS caching, and so when I replace the DLL file in the GAC, (Global Assembly Cache), the change would take effect right away.  This means the IIS 5.1 was not cashing the dll in memory.  This worked great!  So now I have the ability to replace the DLL in the GAC and the IIS server would serve the fresh content right away without having to reset IIS.  However this line does "not" work as far as I have tried in IIS 7.0.

    I tried your suggestion of this as well from the command line prompt, for IIS 7.0:

    appcmd set config "Default Web Site/myWebApp" -section:system.webServer/caching -enabled:false

    and it returned:

    "Applied configuration changes to section......."

    So it executes, but still does not prevent caching of the DLL.  I would like to mimik the same thing I had for IIS 5.1 (in win-xp) for IIS 7.0 (in VISTA), but have spent endless hours wondering how to get it done.  If your suggestion works, I should be able to reoplace a DLL in the GAC with the same name, version and build, and IIS should "not" cache it, therefore allowing the webpage that utilizes that DLL in the GAC to reflect any new changes instantly.  It works in win-xp, so why does it not work in vista?

    **Please help!.... Thanks in advance,



    Monday, August 25, 2008 12:20 AM
  • User511787461 posted

    This thread was about caching of responses on the client - you are talking about dlls on the server in memory - also, ISAPI extensions in the GAC does not make any sense.

    Tuesday, August 26, 2008 5:27 PM
  • User1017348458 posted


    Let me be clear about what I am trying to do.

    Let's suppose that caching ISAPI is enabled for a particualt web app. on the IIS 5.1.  If I place a .dll in the GAC and then call it from that web application in ASP.NET 2.0, then all is fine. 

    However, if I go to rebuild that .dll from within ASP.NET 2.0 to return a different hard coded string, and then replace the original .dll in the GAC, (effectively replacing the oringinal .dll with the same assembly version and build),  IIS does not see the different string when I call the .dll because it caches the .dll.  That is why I use:  "cscript.exe adsutil.vbs set w3svc/1/root/myWebApp/cacheisapi 0" (without quotes), to disable the ISAPI cache.  When this is done, and IIS is reset, IIS 5.1 does not cache the .dll in the GAC anymore.  This means that when no matter how "many" times I rebuild my .dll in ASP.NET 2.0 with different hard coded strings to return and place that new .dll in the GAC, (with the same assembly version and build), the new string will always show when that .dll is being called from any app.  This is becuase caching is turned off at the ISAPI level.

    I have tried this scenario dozens of times, and it certainly resolves the issue of replacing the same .dll in the GAC, (with the same assembly version and build), and still getting the new hard coded string, simply becuase the Cache ISAPI was turned off for that particular website.

    If I were to leave the cache ISAPI on, then IIS caches the .dll and will never retrieve the new string, even after I rebuild my ASP.NET 2.0 .dll and replace it in the GAC with same assembly and version.

    I could use a differenct assembly and build version, instead of turning off the ISAPI cache to resolve it, however I would like to use the same (assembly and build version).

    So the question I pose to you is: how do I turn off ISAPI cache in IIS7, as "cscript.exe adsutil.vbs set w3svc/1/root/myWebApp/cacheisapi 0" (without quotes), does nothing for me in IIS 7?

    thanks ahead of time, theadmirableone

    Thursday, August 28, 2008 11:08 AM
  • User511787461 posted

    Disabling caching of ISAPIs has not been supported since IIS6.

    What you were ending up doing on IIS5.1 is to load and unload all of asp.net and .net framework (and the asp.net worker process) after every request.  You can achieve the same effect by recycling the application pool when you want by running

    %windir%\system32\inetsrv\appcmd.exe recycle apppool "DefaultAppPool" (replace name of app-pool you want)

    Friday, August 29, 2008 4:28 PM
  • User1017348458 posted

    Well Anil.R you are correct!  :~)  Hats-off to you!

    After so many days of trying to get this to work without reseting IIS, your suggestion works. 

    The remaining question is:  If I use your suggested process/command after creating a distinct app pool for this particular web app, (so that the other app pools are not effected), does this command also destroy the session variables that the IIS7 might be currently using, in production, or does it simply clear the cache?

     thanks a million, theadmirableone

    Saturday, August 30, 2008 2:06 PM
  • User-350889266 posted

    Recycling the app pool will destroy any session variables for ASP.NET applications running in that particular app pool but will leave other apps running in other app pools alone.

    Wednesday, September 3, 2008 2:15 PM
  • User887723694 posted

     Thanks! Hopefully this info will help reduce the load on my webserver .. its starting to get a bit overstressed!


    Sunday, October 5, 2008 6:36 PM
  • User2076844711 posted

    Well. Thanks for great information. I have a question though:

     If I enabled static filecaching on a specific directory, i.e:

    appcmd.exe set config "Default Web Site/images" -section:system.webServer/staticContent -clientCache.cacheControlMode:UseMaxAge

    Then, how am I able to remove it again ?

    The funny thing is, I have a .css file in a directory where I did the exact thing above, and max age is set to 84600, but even its the 8. december today, and I make sure my temporary Internet Files etc. is deleted, my browser still sees this .css file as being from the 28. november, even the file is build today. This really worries me, as I see no way to get around this, recycling application pools or not.

    Any help is really appreciated.

    Best, Lars

    Monday, December 8, 2008 9:59 AM
  • User511787461 posted

    This setting only controls client caching of the static content - so anything you do at the server will not purge the cache - the cache has to be purged at the client.

    Wednesday, December 10, 2008 5:49 PM
  • User2006178999 posted

     Anilr, is it possible to set the cache control, for specific folders in a website, programatically in a web.config?

    eg. I wish to add a max-age for 

    • /images
    • /css
    • /javascript
    can this be done in a web.config file?
    Thursday, June 11, 2009 10:07 PM
  • Wednesday, July 15, 2009 9:13 PM
  • User1535959980 posted

    i want to test this features but how can i check it is done  or  not?? 

    if i done, is it meaning,  client can see the static content even i delete the static content in server side???




    i make a test, client (anon user) broswe a page few times(press F5), then i delete the server side image, finally, client cannot see the image


    i followed all the above step:

    C:\Windows\System32\inetsrv>netsh http show  cachestate

    Snapshot of HTTP response cache:

        Status code: 200
        HTTP verb: GET
        Cache policy type: User invalidates
        Creation time: 2010.3.3:2.2.24:0
        Request queue name: DefaultAppPool
        Headers length: 186
        Content length: 184946
        Hit count: 5
        Force disconnect after serving: FALSE


    Windows Server 2008 sp2

    iis 7.0



    Thursday, March 4, 2010 7:10 PM
  • User2135157030 posted

    Do you have to do any kind of commit after you run:

    appcmd.exe set config "Livelink971_2/img" -section:system.webServer/staticContent -clientCache.cacheControlMode:UseMaxAge
    appcmd.exe set config "Livelink971_2/img" -section:system.webServer/staticContent -clientCache.cacheControlMaxAge:"08:00:00"

    I don't see any evidence of client side caching in applicationHost.config or when I analyze with fiddler.  Old IIS 6.0 hardware opens in <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>Total Sequence time:   00:00:05.9364360<o:p></o:p>An new IIS 7.0 hardware with 24 CPUs opens in <o:p></o:p>Total Sequence time:   00:00:16.6388145<o:p></o:p>I can’t seem to figure out client side caching<o:p></o:p>




    Thursday, June 10, 2010 4:04 PM
  • User2006178999 posted


    what are your settings for the following section, in your web.config?


    <serverruntime frequenthitthreshold="1" frequenthittimeperiod="00:00:10"/>

    those are mine. the are _NOT_ the default settings. Maybe this has something to do with it?

    What are those settings? Read about them here.

    Thursday, June 10, 2010 7:57 PM
  • User2103686997 posted

    Hi Anilr

    Would this work for a specific Virtual directory as well?

     appcmd set config /virtualdirectory -section:system.webServer/caching -enabled:false

    I'm trying to disable all caching for aspx pages on a specific virtual directory.

     Thanks Jason

    Thursday, July 1, 2010 6:46 AM
  • User2103686997 posted

    Hi Anil

    Would this work for a specific Virtual directory as well?

     appcmd set config /virtualdirectory -section:system.webServer/caching -enabled:false

    I'm trying to disable all caching for aspx pages on a specific virtual directory.

     Thanks Jason

    Thursday, July 1, 2010 6:49 AM
  • User-1040042518 posted
    Hello , I've stumbled across your forum post and wanted to ask you if you know allow caching and setting a max-age for static files in IIS 6? I know we can added cache-control with a max-age to the whole IIS site, but would like to know if we can specify by the static file extensions? ex) http://xyz.qad.com/jz.jpg ......./jz.png ......../jz.doc ......./jz.pdf ......../jz.js ...../jz.css Any help is fully appreciated. Regards, TRX
    Monday, May 16, 2011 10:14 PM