سؤال

بالنسبة لمشروعي الحالي ، من الضروري إنشاء 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")]) ، فإن الاستجابة هي من نوع المحتوى "النص/html" ، بدلاً من "النص/css" ... عندما أقوم بإزالته ، إنه يعمل كما هو متوقع ...

لذلك ، يبدو لي أن OutputCache لا يحفظ إعداد "contentType" هنا ... هل هناك أي طريقة للتغلب على هذا؟

شكرًا

هل كانت مفيدة؟

المحلول

يمكنك الكتابة فوق 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");
}

أو (وأنا لم أجرب هذا) ولكن تجاوز فئة "OutputCacheAttrible" مع تطبيق محدد 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");
}

نصائح أخرى

هذا يمكن أن يكون خطأ في ASP.NET MVC. داخليا لديهم نوع يسمى OutputCachedPage التي تستمد من Page. متى 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