ASP.NET MVC: проблема с OutputCache

Бесполезные утилиты XAML другой дополнительный автор формы VS Родинки, Karl Shifflett. Генерирует формы, listviews, сетки, базирующиеся на метаданных класса. Также некоторые полезные приемы xaml: группировка элементов в панели, очистка разработчика генерировала код, и т.д.

16
задан David 18 November 2009 в 13:09
поделиться

3 ответа

Вы можете перезаписать 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");
}

Или (и я не пробовал this), но переопределить класс «OutputCacheAttribute» конкретной реализацией CSS. Примерно так ...

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");
}
20
ответ дан 30 November 2019 в 17:39
поделиться

Попробуйте также установить VaryByContentEncoding как VaryByParam.

-1
ответ дан 30 November 2019 в 17:39
поделиться

Это могло быть ошибкой в ​​ASP.NET MVC. Внутренне у них есть тип, называемый OutputCachedPage , производный от Page . Когда OnResultExecuting вызывается в OutputCacheAttribute , они создают экземпляр этого типа и вызывают ProcessRequest (HttpContext.Current) , который в конечном итоге вызывает SetIntprinsics context (HttpContextics) , 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;
   }
}
12
ответ дан 30 November 2019 в 17:39
поделиться
Другие вопросы по тегам:

Похожие вопросы: