IIS 7 and Above
UI & Remote Management
How to set cache control for static content
Last post May 16, 2011 10:14 PM by tnxiong
Jan 25, 2007 04:04 PM|pkorona|LINK
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.
Jan 26, 2007 04:38 PM|anilr|LINK
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"
Nov 23, 2007 08:44 AM|sugarclint|LINK
Feb 27, 2008 02:40 PM|Galcho|LINK
here is a further research on the subject:
IIS7: How to set cache control for static content?
Hope this helps
Jul 12, 2008 12:23 PM|jimrPA|LINK
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?
Jul 16, 2008 01:42 PM|anilr|LINK
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
Jul 16, 2008 05:25 PM|jimrPA|LINK
that's it, thanks
Aug 25, 2008 12:20 AM|theadmirableone|LINK
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,
dll cache iis 7 vista
Aug 26, 2008 05:27 PM|anilr|LINK
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.
Aug 28, 2008 11:08 AM|theadmirableone|LINK
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
Aug 29, 2008 04:28 PM|anilr|LINK
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)
Aug 30, 2008 02:06 PM|theadmirableone|LINK
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
Sep 03, 2008 02:15 PM|andrewbi|LINK
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.
Oct 05, 2008 06:36 PM|afjk|LINK
Thanks! Hopefully this info will help reduce the load on my webserver .. its starting to get a bit overstressed!
Dec 08, 2008 09:59 AM|caslar4711|LINK
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.
Dec 10, 2008 05:49 PM|anilr|LINK
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.
Jun 11, 2009 10:07 PM|pure.krome|LINK
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
Jul 15, 2009 09:13 PM|anilr|LINK
http://www.iis.net/ConfigReference/system.webServer/staticContent/clientCache for examples.
Mar 04, 2010 07:10 PM|windfire|LINK
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
Jun 10, 2010 04:04 PM|jonesch9|LINK
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"
Jun 10, 2010 07:57 PM|pure.krome|LINK
what are your settings for the following section, in your web.config?
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.
Jul 01, 2010 06:46 AM|Jasonnnnnn|LINK
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.
Jul 01, 2010 06:49 AM|Jasonnnnnn|LINK
May 16, 2011 10:14 PM|tnxiong|LINK