我有这个 Action 方法:
[OutputCache(Duration = 2,
Location = OutputCacheLocation.Any,
VaryByHeader = "Accept-Charset")]
public ActionResult Index()
{
return View();
}
生成的响应是:
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
为什么是
Vary
标题显示星号而不是 Accept-Charset
?OutputCacheAttribute
确实对响应有影响,实际上,Expires
和 Cache-Control:max-age=n
header 取决于 Duration
参数,以及 Cache-Control:public
/private
/no-cache
取决于 Location
争论。我为
OutputCacheAttribute
创建了一个包装器看看发生了什么: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();
}
}
标题没有显示在中断中,所以可能是什么
OutputCacheAttribute
确实是配置HttpContext.Current.Response.Cache
.我怎么看
filterContext.HttpContext.Response.Cache.VaryByHeaders.UserCharSet
是 真 ,例如 filterContext.HttpContext.Response.Cache.VaryByHeaders.AcceptTypes
是 假 ,但 Vary
标题总是说 * .我想知道唯一可能的值是否是列为
filterContext.HttpContext.Response.Cache.VaryByHeaders
的属性的四个值。 ,可能吗?干杯。
最佳答案
解决方法是使用 Response.Cache.SetOmitVaryStar(true)
[OutputCache(Duration = 2,
Location = OutputCacheLocation.Any,
VaryByHeader = "Accept-Charset")]
public ActionResult Index()
{
Response.Cache.SetOmitVaryStar(true);
return View("ShowHeaders");
}
我仍在尝试找出 Vary:* 的问题所在:What is the meaning of the HTTP header Vary:*
关于asp.net - OutputCache.VaryByHeader 未在响应中生成 Vary header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7586515/