Asp.Net MVC 3 部分页面输出缓存不尊重配置设置 [英] Asp.Net MVC 3 Partial Page Output Caching Not Honoring Config Settings

查看:20
本文介绍了Asp.Net MVC 3 部分页面输出缓存不尊重配置设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主视图中渲染了一个简单的局部视图:

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.");
    }

在我的配置中:

<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 中我关闭了 enableOutputCacheenableFragmentCache,但它不支持这些设置.

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

奇怪的是,在普通视图中,这些设置工作正常,那么是什么部分视图破坏了这一点?我错过了什么吗?顾说这应该没问题...简而言之,它是否应该尊重 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 源代码.我想到的第一件事是这个功能似乎有点老套.主要是因为他们正在重用一个在一种情况下工作的属性,尊重所有属性和配置设置,然后在子操作场景中忽略所有这些设置,只允许 VaryByParamDuration.

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

这是有异味的主要代码段:

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.

简而言之,部分输出缓存有效,但 BUTT 并不像您想要的那样.我将致力于修复代码并遵守一些设置,例如启用.

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.

这篇关于Asp.Net MVC 3 部分页面输出缓存不尊重配置设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆