OutputCache не кэширует RedirectResult - PullRequest
3 голосов
/ 04 ноября 2011

Кажется, что фильтр выходного кэша не применяется, когда действие контроллера возвращает результат RedirectResult.

Ниже описано, как воспроизвести проблему с веб-приложением ASP.Net MVC3 по умолчанию для Интернета:

В Web.config:

<system.web>
<caching>
<outputCache enableOutputCache="true"></outputCache>
  <outputCacheSettings>
  <outputCacheProfiles>
  <add name="ShortTime" enabled="true" duration="300" noStore="false" />
  </outputCacheProfiles>
  </outputCacheSettings>
  </caching> ...

В HomeController.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcOutputCacheRedir.Controllers
{
    public class HomeController : Controller
    {
        [OutputCache(CacheProfile = "ShortTime")]
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            return View();
        }

        [OutputCache(CacheProfile = "ShortTime")]
        public ActionResult About()
        {

            // Output cache works as expected
            // return View();

            // Output cache has no effect
            return Redirect("Index");
        }
    }
}

Я не могу найти это поведение, указанное где-либо ... это нормально?Если да, то какой обходной путь?

1 Ответ

4 голосов
/ 05 ноября 2011

Это абсолютно намеченное поведение. OutputCacheAttribute используется только для генерирующих строки ActionResults. На самом деле, если вы посмотрите на это (Reflector / ILSpy - ваш друг), вы конкретно увидите это:

string uniqueId = this.GetChildActionUniqueId(filterContext);
string text = this.ChildActionCacheInternal.Get(uniqueId, null) as string;
if (text != null)
{
    filterContext.Result = new ContentResult
    {
        Content = text
    };
    return;
}

Я вижу ваши причины, может быть, даже "обман", приводящий к перенаправлению, может занять много времени / ресурсов, но, похоже, вам придется самостоятельно реализовывать этот вид "кэширования решений".

...