2011-11-04 52 views
3

看来outputcache过滤器在控制器操作返回RedirectResult结果时不适用。OutputCache不缓存RedirectResult

下面是如何重现与ASP.Net MVC3默认的Internet Web应用程序的问题:

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

我无法在任何地方找到此行为......这是正常的吗?如果是这样,任何解决方法?

回答

4

这是绝对有意的行为。 OutputCacheAttribute仅用于字符串生成ActionResults。事实上,如果你想看看它(反射/ 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; 
} 

我可以看到你的理由,甚至“decesion”导致的重定向可以有时间/资源消费,但似乎你将不得不自己实施这种“决策缓存”。

+0

我希望Http内容能被缓存它是一个200或302状态码......谢谢 – 80n