对于我目前的项目有必要生成动态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”)]),所述响应是不是 “文本/ CSS的” 内容类型 “text / html的” 的,... i当删除它,它工作正常...

所以,对我来说似乎的OutputCache不救我“的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");
}

其他提示

这可以是ASP.NET MVC中的错误。 在内部,他们有一种称为OutputCachedPagePage派生。当OnResultExecuting上调用OutputCacheAttribute他们创建此类型和呼叫ProcessRequest(HttpContext.Current),最终调用SetIntrinsics(HttpContext context, bool allowAsync),用于设置的ContentType这样的实例:

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;
   }
}

尝试设置VaryByContentEncoding以及的VaryByParam。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top