6

我有这个动作方法:

    [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确实对响应有影响,实际上,ExpiresandCache-Control:max-age=n标头取决于Duration参数,而//Cache-Control:public取决于参数。privateno-cacheLocation

我创建了一个包装器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是 configure HttpContext.Current.Response.Cache

我可以看到如何filterContext.HttpContext.Response.Cache.VaryByHeaders.UserCharSet真的,例如filterContext.HttpContext.Response.Cache.VaryByHeaders.AcceptTypes的,但Vary标题总是说*

我想知道是否唯一可能的值是列为 的属性的四个值filterContext.HttpContext.Response.Cache.VaryByHeaders,可以吗?

干杯。

4

2 回答 2

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:*

于 2011-09-30T14:56:35.930 回答
0

<%@ OutputCache Duration="2000" VaryByParam="*" VaryByHeader="Accept-Language" %>

于 2011-09-29T16:51:27.090 回答