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

2

6

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.

Homework answered 28/9, 2011 at 16:34 Comment(0)
H
7

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:*

Homework answered 30/9, 2011 at 14:56 Comment(0)
A
0

<%@ OutputCache Duration="2000" VaryByParam="*" VaryByHeader="Accept-Language" %>

Areopagus answered 29/9, 2011 at 16:51 Comment(1)
What is that supposed to do? It sends the Vary:* header as well.Homework

© 2022 - 2024 — McMap. All rights reserved.