首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OutputCache不缓存RedirectResult

OutputCache不缓存RedirectResult
EN

Stack Overflow用户
提问于 2011-11-05 01:07:52
回答 1查看 1.2K关注 0票数 3

当控制器操作返回RedirectResult结果时,outputcache过滤器似乎不适用。

以下是如何重现ASP.Net MVC3默认互联网Web应用程序的问题:

在Web.config中:

代码语言:javascript
复制
<system.web>
<caching>
<outputCache enableOutputCache="true"></outputCache>
  <outputCacheSettings>
  <outputCacheProfiles>
  <add name="ShortTime" enabled="true" duration="300" noStore="false" />
  </outputCacheProfiles>
  </outputCacheSettings>
  </caching> ...

在HomeController.cs中:

代码语言:javascript
复制
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");
        }
    }
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-05 20:05:35

这绝对是故意的行为。OutputCacheAttribute仅用于字符串生成ActionResults。事实上,如果你深入研究它(Reflector/ILSpy是你的朋友),你会发现:

代码语言:javascript
复制
string uniqueId = this.GetChildActionUniqueId(filterContext);
string text = this.ChildActionCacheInternal.Get(uniqueId, null) as string;
if (text != null)
{
    filterContext.Result = new ContentResult
    {
        Content = text
    };
    return;
}

我可以理解你的原因,也许甚至导致重定向的“欺骗”也会耗费时间/资源,但似乎你必须自己实现这种“决策缓存”。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8013157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档