1

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

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

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

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

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

4

1 回答 1

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);
        }
于 2017-11-10T12:14:18.170 回答