Asp.Net MVC 3部分页面输出缓存没有履行配置设置缓存、页面、部分、Asp

2023-09-03 00:55:06 作者:脑子全是你〃

我有一个简单的部分观点,我呈现在我的主要观点有:

I have a simple partial view that I'm rendering in my main view with:

 @Html.Action("All", "Template")

在我的控制器我有这样的:

On my controller I have this:

    [OutputCache(CacheProfile = "Templates")]
    public ActionResult All()
    {
        return Content("This stinks.");
    }

而在我的配置如下:

And in my config this:

<caching>
  <outputCacheSettings>
    <outputCacheProfiles>
      <clear/>
      <add name="Templates" duration="3600" varyByParam="none"/>       
    </outputCacheProfiles>
  </outputCacheSettings>
  <outputCache  enableOutputCache="false" enableFragmentCache="false" />
</caching>

这将无法在运行时异常:

This will fail at runtime with exception:

错误执行的处理子请求System.Web.Mvc.HttpHandlerUtil + ServerExecuteHttpHandlerAsyncWrapper

Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper

和内部异常:

持续时间必须为正数

现在显然不是拿起我的web.config的设置,因为如果我把它改为:

Now obviously it's not picking up my web.config settings, because if I change it to:

[OutputCache(Duration = 3600)]

这会工作,也注意到我的web.config我关掉 enableOutputCache 和 enableFragmentCache ,但它不接受这些设置。

It will work, but also notice in my web.config I turned off enableOutputCache and enableFragmentCache, but it doesn't honor these settings.

奇怪的是,在普通视图这些设置做工精细,所以它是什么有关被打破这一部分的观点?我失去了一些东西? The顾说,这应该只是罚款... 总之,这是应该兑现在web.config中缓存设置,如果没有,为什么不呢?

Curiously, in a normal view these settings work fine, so what is it about partial views that is breaking this? Am I missing something? The Gu says this should work just fine... In short, is it supposed to honor caching settings in web.config and if not, why not?

推荐答案

于是,我花了一分钟,看着MVC 3源。是来找我的第一件事情是这样的功能显得有点哈克。主要是因为它们是重复使用,在一种情况下工作履行所有属性和配置设置,然后在孩子的行动方案只是忽略所有的这些设置,只允许一个属性的的VaryByParam 和持续时间。

So I took a minute and looked at the MVC 3 source. The first thing that came to me was this feature seemed a bit hacky. Mainly because they are reusing an attribute that works in one situation honoring all of the properties and config settings, and then in the child action scenario just ignoring all of those settings and only allowing VaryByParam and Duration.

怎么一会去搞清楚什么支持我是无法理解。因为他们想扔,说的不支持设置永远不会抛出,除非你提供的持续时间和除具备的VaryByParam 值

How one would go about figuring out what is supported is beyond me. Because the exception they want to throw that says Unsupported Setting will never get thrown unless you supplied a duration and a VaryByParam value

下面是主要的一块code的气味:

Here is the main piece of code that smells:

if (Duration <= 0) {
    throw new InvalidOperationException(MvcResources.OutputCacheAttribute_InvalidDuration);
}

if (String.IsNullOrWhiteSpace(VaryByParam)) {
    throw new InvalidOperationException(MvcResources.OutputCacheAttribute_InvalidVaryByParam);
}

if (!String.IsNullOrWhiteSpace(CacheProfile) ||
    !String.IsNullOrWhiteSpace(SqlDependency) ||
    !String.IsNullOrWhiteSpace(VaryByContentEncoding) ||
    !String.IsNullOrWhiteSpace(VaryByHeader) ||
    _locationWasSet || _noStoreWasSet) {
    throw new InvalidOperationException(MvcResources.OutputCacheAttribute_ChildAction_UnsupportedSetting);
}

我不知道这是为什么不叫出的文档,但即使是在API应该说清楚,或者至少抛出正确的异常。

I'm not sure why this isn't called out in documentation, but even if it was the api should make it clear, or at least throw the right exception.

在短期,局部的输出缓存的作品,对接不像你想它。我会努力在固定的code和履行一些像设置的启用

In short, partial output caching works, BUTT not like you would want it too. I'll work on fixing the code and honoring some of the settings like enabled.

更新: 我固定的当前实行至少我的情况与从web.config尊重启用标志,并允许缓存配置文件的工作。 详细在我的博客文章。

Update: I fixed the current implemenation to at least work for my situation with respecting the enabled flag and allowing cache profiles from the web.config. Detailed in my blog post.