【问题标题】:MVC asp.net OutputCache Vary header missing from responseMVC asp.net OutputCache Vary 响应中缺少标头
【发布时间】:2018-04-23 15:58:17
【问题描述】:

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

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

使用 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" 不起作用!

【问题讨论】:

    标签: asp.net-mvc outputcache vary


    【解决方案1】:

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

    【讨论】:

    • 不,这不是 VarybyCustom,而是 VaryByHeaders
    猜你喜欢
    • 2018-03-24
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多