gpt4 book ai didi

ASP.NET MVC : Problem with OutputCache

转载 作者:行者123 更新时间:2023-12-02 08:24:40 27 4
gpt4 key购买 nike

对于我当前的项目,有必要生成动态 CSS...

所以,我有一个充当 CSS 交付者的部分 View ... Controller 代码如下所示:

    [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 MVC : Problem with OutputCache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1755313/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com