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

发布于 2024-12-07 01:27:27 字数 2037 浏览 5 评论 0原文

我有这个操作方法:

    [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/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

我可以看到 filterContext.HttpContext.Response.Cache.VaryByHeaders.UserCharSet 是如何 true 的,例如 filterContext.HttpContext.Response.Cache.VaryByHeaders.AcceptTypes< /code> 为 false,但 Vary 标头始终显示 *

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

干杯。

I have this action method:

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

And the generated response is:

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

Why is the Vary header showing an asterisk instead of Accept-Charset ?

OutputCacheAttribute does have effect on the response, actually, the Expires and Cache-Control:max-age=n headers depends on the Duration argument, and the Cache-Control:public/private/no-cache depends on the Location argument.

I have created a wrapper for OutputCacheAttribute to see what is going on:

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();
    }
} 

The headers are not shown in the break, so probably what OutputCacheAttribute does is configure HttpContext.Current.Response.Cache.

I can see how filterContext.HttpContext.Response.Cache.VaryByHeaders.UserCharSet is true, and for example filterContext.HttpContext.Response.Cache.VaryByHeaders.AcceptTypes is false, but the Vary header always says *.

I am wondering if the only possible values are the four ones listed as properties of filterContext.HttpContext.Response.Cache.VaryByHeaders, could it be?

Cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清泪尽 2024-12-14 01:27:27

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

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

我仍在尝试找出该线程中 Vary:* 出了什么问题:HTTP 标头 Vary 的含义是什么:*

The solution is use Response.Cache.SetOmitVaryStar(true)

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

I am still trying to figure out what is wrong with Vary:* in this thread: What is the meaning of the HTTP header Vary:*

赠佳期 2024-12-14 01:27:27

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文