MVC asp.net OutputCache Vary 标头从响应中丢失



>当我使用 Response.Cache.SetCacheability(HttpCacheability.Private)Response.Cache.VaryByHeaders["someValue"] = true;响应中缺少Vary标头!

虽然Response.Cache.SetCacheability(HttpCacheability.Public)Response.Cache.VaryByHeaders["someValue"] = true;没有问题。它返回变化:一些值

使用 web.config 和控制器属性时出现相同的问题

add name="myCacheProfile" enabled="true" duration="3600" varyByParam="*" varyByHeader="someValue" location="Downstream" 工作并发送 Vary 标头

add name="myCacheProfile" enabled="true" duration="3600" varyByParam="*" varyByHeader="someValue" location="Client" 不起作用!

如果要使用自定义值,则必须在 Global.asax.cs 文件中执行覆盖:

public override string GetVaryByCustomString(HttpContext context, string custom)
        {
            if (!string.IsNullOrEmpty(custom) && context != null && context.Request != null)
            {                
                HttpRequest req = context.Request;
                switch (custom.ToUpper())
                {
                    case "someValue":
                        return req.someValue;
                    case "USER-AGENT":
                        if (req.UserAgent != null && req.Browser != null)
                            return req.UserAgent.ToString() + "-" + req.Browser.IsMobileDevice.ToString() + "-" + req.Browser.Platform;
                        else
                            return base.GetVaryByCustomString(context, custom);
                    default:
                        return base.GetVaryByCustomString(context, custom);
                }
            }
            return base.GetVaryByCustomString(context, custom);
        }

相关内容

  • 没有找到相关文章

最新更新