响应中缺少MVC asp.net OutputCache Vary标头

时间:2017-11-10 12:10:43

标签: asp.net-mvc outputcache vary

我用的时候 Response.Cache.SetCacheability(HttpCacheability.Private)Response.Cache.VaryByHeaders["someValue"] = true; 响应中的Vary标题缺少

Response.Cache.SetCacheability(HttpCacheability.Public)Response.Cache.VaryByHeaders["someValue"] = true;存在问题。它返回Vary:someValue

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

添加name =" myCacheProfile"启用="真"持续时间=" 3600"的VaryByParam =" *" varyByHeader =" someValue中"位置="下游"工作并发送Vary标题

添加name =" myCacheProfile"启用="真"持续时间=" 3600"的VaryByParam =" *" varyByHeader =" someValue中"位置="客户"不起作用!

1 个答案:

答案 0 :(得分:0)

如果您想使用自定义值,则必须在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);
        }
相关问题