Wednesday, January 19, 2011

Force Fresh Images on IIS 7

How do I force IIS 7 to not cache images on a certain page?

  • I don't think the IIS web server is the one caching pages - it's the client's browser.

    You can add a meta tag to the pages you don't want the client side to cache, and there are ways to do this for different older browsers and such.

    If you write in ASP and want the same non-cache effect, here's the header information.

    <% Response.CacheControl = "no-cache" %>>
    <% Response.AddHeader "Pragma", "no-cache" %>
    <% Response.Expires = -1 %>
    
    Chris W. Rea : Those Response directives will only set the headers for ASP pages, not for static images. IIS settings need to be adjusted for static images.
    From Mike
  • The thing you are looking for is cache-control header value (note that this only works for browsers that respect http 1.1)

    For asp the code is:

    <% @Language="VBScript" %>
    <% Response.CacheControl = "no-cache" %>
    

    You can also set this directly on a folder using the metabase:

    Here's how you would set the folder pix on the default website: Open a command prompt and change to your C:\InetPub\AdminScripts folder. Run the following command: CSCRIPT ADSUTIL.VBS SET W3SVC/1/ROOT/pix/CacheControlCustom "no-cache"

    Note the possible values are "no-cache" , "Public", "Private"

    Yo can also set this via ADSI:

    Option Explicit
    Dim objCache
    Set objCache = GetObject("IIS://localhost/w3svc/1/root/pix")
    objCache.CacheControlCustom = "no-cache"
    objCache.SetInfo
    

    So far these approaches will work on IIS6 and IIS7 so long as you have the IIS6 admin tools installed. For a pure IIS7 environment here are the appcmd commands:

    First unlock the config section

    appcmd unlock config /section:staticContent
    

    Now you're good to change the caching options for static content. Make static content non-cacheable by setting "Cache-Control: no-cache":

    appcmd set config "Default Web Site/<Application>/<Folder>" /section:staticContent /clientCache.cacheControlMode:DisableCache
    

    Where <Application>/<Folder> is the path to your folder

    See also IIS 7.0: clientCache Element for staticContent (IIS Settings Schema)

    From Jim B

0 comments:

Post a Comment