对于我目前的项目,有必要生成动态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,该缓冲区在缓存发生后执行.
public class CustomContentTypeAttribute : ActionFilterAttribute { public string ContentType { get; set; } public override void OnResultExecuted(ResultExecutedContext filterContext) { filterContext.HttpContext.Response.ContentType = ContentType; } }
[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"); }