4

It seems outputcache filter does not apply when a controller action returns a RedirectResult result.

Here is how to reproduce the problem with ASP.Net MVC3 default Internet Web Application :

In Web.config :

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

In 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");
        }
    }
}

I cannot find this behavior specified anywhere ... is this normal? If so, any workaround ?

80n
  • 43
  • 4

1 Answers1

5

That is absolutely intended behaviour. OutputCacheAttribute is used only for string-generating ActionResults. In fact, if you would look into it (Reflector/ILSpy is your friend), you would specifically see this :

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

I can see your reasons, maybe even "decesion" resulting in redirect can be time/resource consuming, but it seems you will have to implement this kind of "decision-caching" yourself.

rouen
  • 5,003
  • 2
  • 25
  • 48
  • I expected the Http content to be cached wether it was a 200 or a 302 status code ... Thanks – 80n Nov 07 '11 at 08:53