加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.NET MVC:OutputCache的问题

发布时间:2020-12-15 23:44:51 所属栏目:asp.Net 来源:网络整理
导读:对于我目前的项目,有必要生成动态CSS … 所以,我有一个部分视图作为一个CSS提供者…控制器代码看起来像这样: [OutputCache(CacheProfile = "DetailsCSS")] public ActionResult DetailsCSS(string version,string id) { // Do something with the version a
对于我目前的项目,有必要生成动态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;
    }
}

然后在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");
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读