【问题标题】:OutputCache does not cache RedirectResultOutputCache 不缓存 RedirectResult
【发布时间】:2011-11-04 17:07:52
【问题描述】:

当控制器操作返回 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");
        }
    }
}

我在任何地方都找不到这种行为……这正常吗?如果是这样,有什么解决方法吗?

【问题讨论】:

    标签: asp.net-mvc outputcache response.redirect


    【解决方案1】:

    这是绝对预期的行为。 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;
    }
    

    我可以看到您的原因,甚至可能导致重定向的“决策”可能会耗费时间/资源,但您似乎必须自己实现这种“决策缓存”。

    【讨论】:

    • 我希望 Http 内容无论是 200 还是 302 状态码都会被缓存...谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 2010-10-11
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 2011-12-10
    相关资源
    最近更新 更多