2011-11-04 46 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

相關問題