Asp.Net MVC 3部分页面输出缓存不符合配置设置

Gre*_*rts 7 .net iis asp.net-mvc caching

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

 @Html.Action("All", "Template")
Run Code Online (Sandbox Code Playgroud)

在我的控制器上我有这个:

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

在我的配置中:

<caching>
  <outputCacheSettings>
    <outputCacheProfiles>
      <clear/>
      <add name="Templates" duration="3600" varyByParam="none"/>       
    </outputCacheProfiles>
  </outputCacheSettings>
  <outputCache  enableOutputCache="false" enableFragmentCache="false" />
</caching>
Run Code Online (Sandbox Code Playgroud)

这将在运行时失败,但有异常:

执行处理程序'System.Web.Mvc.HttpHandlerUtil + ServerExecuteHttpHandlerAsyncWrapper的子请求时出错

内在异常:

持续时间必须是正数

现在显然它没有拿起我的web.config设置,因为如果我将其更改为:

[OutputCache(Duration = 3600)]
Run Code Online (Sandbox Code Playgroud)

它会工作,而且在我的web.config通知我关掉enableOutputCacheenableFragmentCache,但它不支持这些设置.

奇怪的是,在普通视图中这些设置工作正常,那么部分视图是什么打破了这个呢?我错过了什么吗?顾说这应该工作得很好...... 总之,它是否应该尊重web.config中的缓存设置,如果没有,为什么不呢?

Gre*_*rts 5

所以我花了一分钟看了MVC 3的来源.我遇到的第一件事就是这个功能看起来有些笨拙.主要是因为他们重用一个在一种情况下工作的属性来表示所有属性和配置设置,然后在子操作场景中忽略所有这些设置并且只允许VaryByParamDuration.

如何确定支持的内容超出了我的范围.因为除非你提供了一个持续时间和一个VaryByParam值,否则他们想抛出的异常会说永远不会抛出Unsupported Setting

这是代码的主要代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

我不确定为什么在文档中没有调用它,但即使它是api也应该清楚,或者至少抛出正确的异常.

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

更新: 我修改了当前的实现,至少可以解决启用标志并允许来自web.config的缓存配置文件. 详情请参阅我的博文.