开发者

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 开发者_Go百科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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜