J.P.
J.P.

Reputation: 41

MVC asp.net OutputCache Vary header missing from response

When I use Response.Cache.SetCacheability(HttpCacheability.Private) and Response.Cache.VaryByHeaders["someValue"] = true; the Vary header is missing from the response!

There is NO problem with the Response.Cache.SetCacheability(HttpCacheability.Public) and Response.Cache.VaryByHeaders["someValue"] = true; although. It Returns Vary: someValue

The same problem when using web.config and controller attributes

add name="myCacheProfile" enabled="true" duration="3600" varyByParam="*" varyByHeader="someValue" location="Downstream" Works and sends the Vary header

add name="myCacheProfile" enabled="true" duration="3600" varyByParam="*" varyByHeader="someValue" location="Client" doesnt work!

Upvotes: 1

Views: 203

Answers (1)

Axel Andersen
Axel Andersen

Reputation: 350

If you wanna use custom values, you must do an override in the Global.asax.cs file:

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);
        }

Upvotes: 0

Related Questions