当控制器操作返回RedirectResult结果时,outputcache过滤器似乎不适用。
以下是如何重现ASP.Net MVC3默认互联网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");
}
}
}我在任何地方都找不到指定的行为...这是正常的吗?如果是这样,有什么解决方法吗?
发布于 2011-11-05 20:05:35
这绝对是故意的行为。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;
}我可以理解你的原因,也许甚至导致重定向的“欺骗”也会耗费时间/资源,但似乎你必须自己实现这种“决策缓存”。
https://stackoverflow.com/questions/8013157
复制相似问题