OutputCache.VaryByHeader未在响应中生成Vary标头

时间:2011-09-28 16:35:00

标签: asp.net asp.net-mvc caching outputcache http-caching

我有这个动作方法:

    [OutputCache(Duration = 2, 
                 Location = OutputCacheLocation.Any, 
                 VaryByHeader = "Accept-Charset")]
    public ActionResult Index()
    {
        return View();
    }

生成的响应是:

Cache-Control:public, max-age=2
Content-Length:5164
Content-Type:text/html; charset=utf-8
Date:Wed, 28 Sep 2011 16:30:33 GMT
Expires:Wed, 28 Sep 2011 16:30:35 GMT
Last-Modified:Wed, 28 Sep 2011 16:30:33 GMT
Server:Microsoft-IIS/7.5
Vary:*
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:3.0
X-Powered-By:ASP.NET

为什么Vary标题显示的是星号而不是Accept-Charset

OutputCacheAttribute确实会对响应产生影响,实际上,ExpiresCache-Control:max-age=n标头取决于Duration参数,Cache-Control:public / {{ 1}} / private取决于no-cache参数。

我为Location创建了一个包装器,看看发生了什么:

OutputCacheAttribute

标题不会显示在中断中,因此public class CustomOutputCacheAttribute:OutputCacheAttribute { public override void OnResultExecuted(ResultExecutedContext filterContext) { base.OnResultExecuted(filterContext); Dictionary<String, String> headers = new Dictionary<string, string>(); foreach (var header in filterContext.HttpContext.Response.Headers.AllKeys) headers.Add(header, filterContext.HttpContext.Response.Headers[header]); Debugger.Break(); } } 可能会配置OutputCacheAttribute

我可以看到HttpContext.Current.Response.Cache true 的方式,例如filterContext.HttpContext.Response.Cache.VaryByHeaders.UserCharSet false ,但filterContext.HttpContext.Response.Cache.VaryByHeaders.AcceptTypes标题始终显示 *

我想知道唯一可能的值是否是列为Vary属性的四个值,可能是吗?

干杯。

2 个答案:

答案 0 :(得分:7)

解决方案是使用Response.Cache.SetOmitVaryStar(true)

    [OutputCache(Duration = 2,
         Location = OutputCacheLocation.Any,
         VaryByHeader = "Accept-Charset")]
    public ActionResult Index()
    {
        Response.Cache.SetOmitVaryStar(true);
        return View("ShowHeaders");
    }

我仍然想弄清楚Vary有什么问题:*在这个帖子中:What is the meaning of the HTTP header Vary:*

答案 1 :(得分:0)

&lt;%@ OutputCache Duration =“2000”VaryByParam =“*”VaryByHeader =“Accept-Language”%&gt;

相关问题