Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OutputCache.VaryByHeader is not generating a Vary header in the response

I have this action method:

    [OutputCache(Duration = 2, 
                 Location = OutputCacheLocation.Any, 
                 VaryByHeader = "Accept-Charset")]
    public ActionResult Index()
    {
        return View();
    }

And the generated response is:

Cache-Control:public, max-age=2
Content-Length:5164
Content-Type:text/html; charset=utf-8
Date:Wed, 28 Sep 2011 16:30:33 GMT
Expires:Wed, 28 Sep 2011 16:30:35 GMT
Last-Modified:Wed, 28 Sep 2011 16:30:33 GMT
Server:Microsoft-IIS/7.5
Vary:*
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:3.0
X-Powered-By:ASP.NET

Why is the Vary header showing an asterisk instead of Accept-Charset ?

OutputCacheAttribute does have effect on the response, actually, the Expires and Cache-Control:max-age=n headers depends on the Duration argument, and the Cache-Control:public/private/no-cache depends on the Location argument.

I have created a wrapper for OutputCacheAttribute to see what is going on:

public class CustomOutputCacheAttribute:OutputCacheAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        base.OnResultExecuted(filterContext);

        Dictionary<String, String> headers = new Dictionary<string, string>();
        foreach (var header in filterContext.HttpContext.Response.Headers.AllKeys)
            headers.Add(header, filterContext.HttpContext.Response.Headers[header]);

        Debugger.Break();
    }
} 

The headers are not shown in the break, so probably what OutputCacheAttribute does is configure HttpContext.Current.Response.Cache.

I can see how filterContext.HttpContext.Response.Cache.VaryByHeaders.UserCharSet is true, and for example filterContext.HttpContext.Response.Cache.VaryByHeaders.AcceptTypes is false, but the Vary header always says *.

I am wondering if the only possible values are the four ones listed as properties of filterContext.HttpContext.Response.Cache.VaryByHeaders, could it be?

Cheers.

like image 973
vtortola Avatar asked Sep 28 '11 16:09

vtortola


1 Answers

The solution is use Response.Cache.SetOmitVaryStar(true)

    [OutputCache(Duration = 2,
         Location = OutputCacheLocation.Any,
         VaryByHeader = "Accept-Charset")]
    public ActionResult Index()
    {
        Response.Cache.SetOmitVaryStar(true);
        return View("ShowHeaders");
    }

I am still trying to figure out what is wrong with Vary:* in this thread: What is the meaning of the HTTP header Vary:*

like image 135
vtortola Avatar answered Sep 21 '22 17:09

vtortola