加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – OutputCache.VaryByHeader没有在响应中生成Vary标头

发布时间:2020-12-16 06:30:04 所属栏目:asp.Net 来源:网络整理
导读:我有这个动作方法: [OutputCache(Duration = 2,Location = OutputCacheLocation.Any,VaryByHeader = "Accept-Charset")] public ActionResult Index() { return View(); } 生成的响应是: Cache-Control:public,max-age=2Content-Length:5164Content-Type:t
我有这个动作方法:

[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确实对响应有影响,实际上,Expires和Cache-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为false,但Vary标头总是说*.

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

干杯.

解决方法

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读