Search code examples
asp.netiisiis-7iis-7.5httpmodule

IIS 7 classic mode Set-MaxAge in a custom HttpModule


I am implementing a caching mechanism. I have written a HTTPModule which would intercept all the response and suffix a buildnumber to the static files. And also rewrite the url in the request by stripping of the build number.

I want to set the MaxAge in the response to a future date, say a year. But when I see it in fiddler it is not setting the max age. I also tried setting the expiry but doesn't seem to work.

It works fine in IIS 7 integrated mode but not in Classic mode.

 context.Response.Cache.SetCacheability(HttpCacheability.Public);                   
 context.Response.Cache.SetMaxAge(new TimeSpan(DateTime.Now.AddYears(1).Ticks));
 context.Response.Cache.SetExpires(DateTime.Now.AddYears(2));
 context.Response.AddHeader("Expires", DateTime.Now.AddYears(1).ToShortDateString());

None of these seem to make an impact on the cache settings. What is the best way to achieve this ? I don't want to use Integrated mode.

Update with header information:

HTTP/1.1 200 OK 
Cache-Control: public 
Content-Type: image/gif 
Expires: Fri, 23 Dec 2011 14:53:12 GMT 
Last-Modified: Mon, 21 Nov 2011 11:50:11 GMT 
Accept-Ranges: bytes 
ETag: "1CCA843B92E5B80" 
Server: Microsoft-IIS/7.5 
X-AspNet-Version: 4.0.30319 
X-Powered-By: ASP.NET 
Date: Thu, 22 Dec 2011 14:53:12

The response headers when the cache control set to private

HTTP/1.1 200 OK
Cache-Control: private, max-age=31536000
Content-Length: 2157
Content-Type: text/css
Expires: Sat, 24 Dec 2011 09:03:41 GMT
Last-Modified: Mon, 21 Nov 2011 11:50:09 GMT
Accept-Ranges: bytes
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 23 Dec 2011 09:03:41 GMT

I have included the code which I am using

  context.BeginRequest += new EventHandler(this.AddCacheExpiry);

  private void AddCacheExpiry(object sender, EventArgs e)
    {
        HttpApplication application = (HttpApplication)sender;
        HttpContext context = application.Context;           

        if (context.Request.AppRelativeCurrentExecutionFilePath.IndexOf(BuildNumber) != -1)
        {
                context.Response.Cache.SetCacheability(HttpCacheability.Private);                   
                context.Response.Cache.SetMaxAge(new TimeSpan(DateTime.Now.AddYears(1).Ticks));
                context.Response.Cache.SetExpires(DateTime.Now.AddYears(2));
                context.Response.Cache.SetLastModifiedFromFileDependencies(); 
        }
    }

Solution

  • I ran into the same problem on Win2k8 IIS7. My resolution was to ensure that I commented out caching references in the system.webServer section of web.config:

    <!--<caching>
      <profiles>
        <add extension=".ico" kernelCachePolicy="CacheUntilChange" />
        <add extension=".css" kernelCachePolicy="CacheUntilChange" />
        <add extension=".gif" kernelCachePolicy="CacheUntilChange" />
        <add extension=".js" kernelCachePolicy="CacheUntilChange" />
      </profiles>
    </caching>-->
    
    <staticContent>
      <!--<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />-->
    </staticContent>
    

    Also make sure you have no <%@ OutputCache %> directives on the Page handler if you are using one, and that you are not overriding the cache with Response.Expires etc.

    Output caching will always override your Response.Cache settings it seems.