对于我当前的项目,有必要生成动态CSS ...
所以,我有一个局部视图,可以用作CSS传递器...控制器代码如下所示:
[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");
}
输出缓存配置文件如下所示:
<add name="DetailsCSS" duration="360" varyByParam="*" location="Server" varyByContentEncoding="none" varyByHeader="none" />
问题是:当我使用OutputCache行([OutputCache(CacheProfile =“ DetailsCSS”)])时,响应的内容类型为“ text / html”,而不是“ text / css” ...当我删除它时,它按预期工作...
因此,对我来说,OutputCache似乎没有在这里保存我的“ ContentType”设置...有没有解决的办法?
谢谢
最佳答案
您可以使用自己的ActionFilter覆盖ContentType,该ActionFilter在缓存发生后执行。
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");
}
关于asp.net - ASP.NET MVC:OutputCache问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1755313/