ASP.NET MVC:OutputCache存在问题
对于我当前的项目,有必要生成动态CSS 所以,我有一个局部视图,作为CSS交付者。。。控制器代码如下所示:ASP.NET MVC:OutputCache存在问题,asp.net,asp.net-mvc,outputcache,Asp.net,Asp.net Mvc,Outputcache,对于我当前的项目,有必要生成动态CSS 所以,我有一个局部视图,作为CSS交付者。。。控制器代码如下所示: [OutputCache(CacheProfile = "DetailsCSS")] public ActionResult DetailsCSS(string version, string id) { // Do something with the version and id here.... bla bla Response
[OutputCache(CacheProfile = "DetailsCSS")]
public ActionResult DetailsCSS(string version, string id)
{
// Do something with the version and id here.... bla bla
Response.ContentType = "text/css";
return PartialView("_css");
}
HttpCapabilitiesBase browser = this._request.Browser;
this._response.ContentType = browser.PreferredRenderingMime;
输出缓存配置文件如下所示:
<add name="DetailsCSS" duration="360" varyByParam="*" location="Server" varyByContentEncoding="none" varyByHeader="none" />
问题是:当我使用OutputCache行([OutputCache(CacheProfile=“DetailsCSS”)])时,响应的内容类型是“text/html”,而不是“text/css”。。。当我移除它时,它会按预期工作
所以,对我来说,OutputCache似乎没有在这里保存我的“ContentType”设置。。。这有什么办法吗
感谢您尝试设置VaryByContentEncoding和VaryByParam。这可能是ASP.NET MVC中的一个错误。 在内部,它们有一个名为
OutputCachedPage
的类型,该类型派生自Page
。在OutputCacheAttribute
上调用OnResultExecuting
时,它们会创建此类型的实例,并调用ProcessRequest(HttpContext.Current)
,最终调用SetIntrinsics(HttpContext,bool allowAsync)
,设置如下内容类型:
[OutputCache(CacheProfile = "DetailsCSS")]
public ActionResult DetailsCSS(string version, string id)
{
// Do something with the version and id here.... bla bla
Response.ContentType = "text/css";
return PartialView("_css");
}
HttpCapabilitiesBase browser = this._request.Browser;
this._response.ContentType = browser.PreferredRenderingMime;
这里有一个解决方案:
public sealed class CacheAttribute : OutputCacheAttribute {
public override void OnResultExecuting(ResultExecutingContext filterContext) {
string contentType = null;
bool notChildAction = !filterContext.IsChildAction;
if (notChildAction)
contentType = filterContext.HttpContext.Response.ContentType;
base.OnResultExecuting(filterContext);
if (notChildAction)
filterContext.HttpContext.Response.ContentType = contentType;
}
}
您可以使用自己的ActionFilter覆盖ContentType,该过滤器在缓存发生后执行
public class CustomContentTypeAttribute : ActionFilterAttribute
{
public string ContentType { get; set; }
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.HttpContext.Response.ContentType = ContentType;
}
}
然后在OutputCache之后调用该属性
[CustomContentType(ContentType = "text/css", Order = 2)]
[OutputCache(CacheProfile = "DetailsCSS")]
public ActionResult DetailsCSS(string version, string id)
{
// Do something with the version and id here.... bla bla
return PartialView("_css");
}
或者(我还没有尝试过),但使用特定于CSS的实现重写“OutputCacheAttribute”类。像这样的
public class CSSOutputCache : OutputCacheAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
base.OnResultExecuting(filterContext);
filterContext.HttpContext.Response.ContentType = "text/css";
}
}
而这个
[CSSOutputCache(CacheProfile = "DetailsCSS")]
public ActionResult DetailsCSS(string version, string id)
{
// Do something with the version and id here.... bla bla
return PartialView("_css");
}
哎呀。是的,那不行。ContentType!=内容编码!!抱歉,我的错。我更喜欢CSSOutputCacheAttribute版本(注意,您的示例缺少类名末尾的属性)。我已经对它进行了测试,它工作正常,看起来很不错:)。检查
filterContext.IsChildAction
的意义是什么?