80n*_*80n 4 asp.net-mvc response.redirect 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> ...
Run Code Online (Sandbox Code Playgroud)
在 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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在任何地方都找不到指定的这种行为......这正常吗?如果是这样,有什么解决方法吗?
这绝对是有意的行为。OutputCacheAttribute 仅用于生成字符串的 ActionResult。事实上,如果你仔细研究一下(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;
}
Run Code Online (Sandbox Code Playgroud)
我可以看到你的原因,也许甚至导致重定向的“决定”可能会消耗时间/资源,但似乎你必须自己实现这种“决策缓存”。
| 归档时间: |
|
| 查看次数: |
1291 次 |
| 最近记录: |