Ben*_*key 11 c# caching asp.net-mvc-3
使用ASP.NET MVC 3中的[OutputCacheAttribute],您可以非常灵活地输出缓存.利用'VaryByHeader'属性按主机名进行缓存是很有用的.例如:
[OutputCache(Duration = 60, VaryByHeader = "host")]
public ActionResult Foo()
{
return this.View();
}
Run Code Online (Sandbox Code Playgroud)
但是,对于子操作,您无法应用'VaryByHeader'.该框架抛出以下异常:
子操作的OutputCacheAttribute仅支持Duration,VaryByCustom和VaryByParam值.请不要为子操作设置CacheProfile,Location,NoStore,SqlDependency,VaryByContentEncoding或VaryByHeader值.
我的问题是,为什么?
我们不能在子操作中使用VaryByHeader的原因是因为它会提供冲突的方差,因为父操作可能指定了不同的VaryByHeader值?
如果我想根据主机名缓存子操作,这意味着什么,我将如何处理它?
bha*_*lin 13
VaryByHeader影响实际的HTTP响应标头; 所以你可能是正确的,因为MVC团队阻止了这一点,以防止与父行动发生冲突.
要根据主机名缓存,你能不能使用VaryByCustom?像(免责声明:根本没试过):
[OutputCache(Duration = 60, VaryByCustom = "host")]
public ActionResult Foo()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
然后是(在你的Global.asax.cs中)
public override string GetVaryByCustomString(HttpContext context, string arg)
{
if (arg == "host")
{
return context.Request.Headers["host"];
}
// whatever you have already, or just String.Empty
return String.Empty;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5005 次 |
| 最近记录: |