Search code examples
silverlightiis-7http-headerswmicache-expiration

Trying to Programmatically Expire the HTTP Response Headers


Our Team is building a C# project with a Silverlight module. We deploy to a Windows 2008 with IIS 7. I’m trying to Programmatically Expire the HTTP Response Headers Associated with a Folder called ClientBin immediately. I know how to do it manually through IIS Manager. ( Basically, I go to the HTTP Response Headers Section of the folder or file that is of interest, and then I use "Set Common Headers...." to expire immediately.) However, we will be Redeploying to IIS a number of times, and I want to ensure that it is programmatically done because it’s a headache to keep Reconfiguring all the time.

Should I do it from the C# code of my project or is it better practice to do it using WMI scripting?


Solution

  • @kev and @jeff-cuscutis have provided the ways to configure expiration of the HTTP Response Headers using XML configuration in the web.config file of a ASP.NET application

    How to configure static content cache per folder and extension in IIS7?

    ou can set specific cache-headers for a whole folder in either your root web.config:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <configuration>
    
    <!-- Note the use of the 'location' tag to specify which 
    
       folder this applies to-->
    
    <location path="images">
    
    <system.webServer>
    
      <staticContent>
    
        <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:00:15" />
    
      </staticContent>
    
    </system.webServer>
    
    </location>
    
    </configuration>
    

    Or you can specify these in a web.config file in the content folder:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <configuration>
    
    <system.webServer>
    
    <staticContent>
    
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:00:15" />
    
    </staticContent>
    
    </system.webServer>
    
    </configuration>
    

    I'm not aware of a built in mechanism to target specific file types.

    You can do it on a per file basis. Use the path attribute to include the filename

    <?xml version="1.0" encoding="UTF-8"?>
    
    <configuration>
    
    <location path="YourFileNameHere.xml">
    
        <system.webServer>
    
            <staticContent>
    
                <clientCache cacheControlMode="DisableCache" />
    
            </staticContent>
    
        </system.webServer>
    
    </location>
    
    </configuration>