【发布时间】:2011-12-21 16:52:04
【问题描述】:
我正在实现一个缓存机制。我编写了一个 HTTPModule,它会拦截所有响应并为静态文件添加一个内部版本号。并且还通过剥离内部版本号来重写请求中的 url。
我想在对未来日期的响应中设置 MaxAge,比如一年。但是当我在提琴手中看到它时,它并没有设置最大年龄。我也尝试设置过期时间,但似乎不起作用。
它在 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();
}
}
【问题讨论】:
-
你能发布你的http头是什么样的吗?集成和经典模式会将最大年龄放在标题的不同部分。 “缓存控制”与“过期”
-
HTTP/1.1 200 OK 缓存控制:public 内容类型:image/gif 过期时间:2011 年 12 月 23 日星期五 14:53:12 GMT 最后修改时间:2011 年 11 月 21 日星期一 11:50 :11 GMT 接受范围:字节 ETag:“1CCA843B92E5B80” 服务器:Microsoft-IIS/7.5 X-AspNet-Version:4.0.30319 X-Powered-By:ASP.NET 日期:2011 年 12 月 22 日星期四 14:53:12格林威治标准时间
-
默认设置为次日到期。不确定这是否由 IIS 完成
标签: asp.net iis iis-7 iis-7.5 httpmodule