asp.net - OutputCache.VaryByHeader 未在响应中生成 Vary header

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

我有这个 Action 方法:

    [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 header 取决于 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 ,例如 filterContext.HttpContext.Response.Cache.VaryByHeaders.AcceptTypes ,但 Vary标题总是说 * .

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

干杯。

最佳答案

解决方法是使用 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:*

关于asp.net - OutputCache.VaryByHeader 未在响应中生成 Vary header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7586515/

相关文章:

c# - 如何从 javascript 调用 asp.net .cs 页面的方法?

asp.net-mvc - 基于 HTML5 的文档(doc/xls)查看器和编辑器 : What are my options?

c# - ASP.NET Core 2.0+ 中的 Multi-Tenancy

Android Ion 缓存刷新

asp.net - 参数未传递到 ssrs ServerReport - 使用 ServerReport.Render 将报告渲染为 pdf

asp.net - 如何将.net 4.6项目转换为5.0

asp.net - 在代码隐藏文件 C# 中将 boundField 添加到 gridview

asp.net-mvc - ASP.Net MVC 中的两步验证

caching - 禁用 Web API 响应的缓存

http - Chrome 和过期 header - 图像缓存