我正在实现一个缓存机制。我已经写了一个HTTPModule,它将拦截所有的响应和后缀一个构建号到静态文件。并且通过剥离构建号来重写请求中的url。
我想将响应中的MaxAge设置为未来的日期,例如一年。但是当我在fiddler中看到它时,它没有设置最大年龄。我也试过设置过期,但似乎不起作用。
在IIS 7集成模式下可以正常工作,但在经典模式下不行。
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());
这些似乎都不会对缓存设置产生影响。实现这一目标的最佳方式是什么?我不想使用集成模式。
更新头信息:
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
当缓存控制设置为私有
时的响应头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
我已经包含了我正在使用的代码
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();
}
}
我在Win2k8 IIS7上遇到了同样的问题。我的解决方案是确保在系统中注释掉缓存引用。web.config的webServer部分:
<!--<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>
还要确保在Page处理程序中没有<%@ OutputCache %>指令(如果您正在使用),并且您没有使用Response覆盖缓存。到期等。
输出缓存将始终覆盖您的响应。好像是缓存设置
好像有什么东西覆盖了你的设置。
当我在经典模式下使用你的设置时,我得到了你的代码想要的。我也想知道你是否有一个HttpModule或HttpHandler也修改你的头。我的直觉是其他代码正在覆盖您的响应设置。
检查此链接以确保IIS没有设置错误:http://technet.microsoft.com/en-us/library/cc770661 (WS.10) . aspx