使用 OutputCacheAttribute 时忽略 SetLastModified
我有一个 ASP.NET MVC 方法(.NET 4.0 上的 v3.0)设置如下:
[OutputCache(Duration = 31536000, Location = OutputCacheLocation.Any)]
public virtual ActionResult Item()
{
this.Response.Cache.SetLastModified(new DateTime(2011, 01, 01));
return this.Content("hello world", "text/plain");
}
我希望它返回时将 Last-Modified
标头设置为 Mon, 07 Feb 2011 00:00:00 GMT
如指定,但实际上返回的是输出首次缓存在输出缓存中的日期(即自 IIS 以来第一次调用该方法)重置)。
如果我注释掉 [OutputCache]
属性,以便不进行输出缓存,那么 Last-Modified
标头会按预期返回,因此看起来它是输出中的内容缓存基础设施选择忽略我为此指定的值。
知道为什么会这样做吗?有没有办法让它使用我指定的值作为Last-Modified
日期?
I've got an ASP.NET MVC method (v3.0 on .NET 4.0) set up like the following:
[OutputCache(Duration = 31536000, Location = OutputCacheLocation.Any)]
public virtual ActionResult Item()
{
this.Response.Cache.SetLastModified(new DateTime(2011, 01, 01));
return this.Content("hello world", "text/plain");
}
I'd expect this to come back with the Last-Modified
header set to Mon, 07 Feb 2011 00:00:00 GMT
as specified, however it is actually coming back as the date that the output was first cached in the output cache (i.e. the first time the method was called since IIS was reset).
If I comment out the [OutputCache]
attribute so that no output caching is done then the Last-Modified
header comes back as expected, so it appears that it's something in the output caching infrastructure that's choosing to ignore my specified value for this.
Any idea why it might be doing so? And is there any way to make it use my specified value as the Last-Modified
date?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我从来没有弄清楚发生这种情况的原因,但它看起来像是
[OutputCache]
属性使用的 ASP.NET 页面缓存基础结构中的某个错误。我最终编写了一个具有大致相同公共接口的自定义
[HttpCache]
属性,但它直接在Response.Cache
对象上调用适当的缓存方法,而不是委托给ASP.NET 页面缓存基础结构。效果很好。遗憾的是内置属性没有。
Well, I never did work out the reason this occurs, but it looks like a bug somewhere in the ASP.NET page caching infrastructure which the
[OutputCache]
attribute uses.I ended up writing a custom
[HttpCache]
attribute with much the same public interface, but which calls the appropriate cache methods on theResponse.Cache
object directly rather than delegating to the ASP.NET page caching infrastructure.That works fine. It's a shame the built-in attribute doesn't.
在控制器的 OnResultExecuting 事件期间,[OutputCache] 创建 System.Web.UI.Page 的实例来处理属性中指定的缓存属性。他们这样做是因为 Page 已经具有将 OutputCacheParameters 转换为实际 http 缓存指令的逻辑。
https://aspnetwebstack.codeplex.com/SourceControl/latest #src/System.Web.Mvc/OutputCacheAttribute.cs
OutputCacheAttribute 基本上将原始处理程序(控制器)的输出复制到为配置缓存而创建的页面。
这里的缺点是添加到原始 HttpResponse 的标头不会复制到新的处理程序 (Page)。这意味着无法在控制器中的响应上设置标头。实际处理请求的 Page 会忽略这些标头。
During the Controller's OnResultExecuting event, [OutputCache] creates an instance of System.Web.UI.Page to handle the cache properties specified in the attribute. They do this because Page already has logic to translate OutputCacheParameters to actual http cache directives.
https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/OutputCacheAttribute.cs
The OutputCacheAttribute basically copies the output from the original handler (controller) to the Page created to configure the cache.
The disadvantage here is that headers added to the original HttpResponse do not get copied over to the new handler (Page). This means it's impossible to set headers on the Response in the controller. The Page that actually processes the request ignores these headers.