我有一个简单的局部视图,我在主视图中渲染:
- @Html.Action("All","Template")
在我的控制器上我有这个:
- [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>
这将在运行时失败,但有异常:
Error executing child request for handler ‘System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper
内在的例外:
Duration must be a positive number
现在显然它没有拿起我的web.config设置,因为如果我将其更改为:
- [OutputCache(Duration = 3600)]
它会工作,但在我的web.config中也会注意到我关闭了enableOutputCache和enableFragmentCache,但是它没有遵循这些设置.
奇怪的是,在普通视图中这些设置工作正常,那么部分视图是什么打破了这个呢?我错过了什么吗? The Gu says this should work just fine…
简而言之,是否应该尊重web.config中的缓存设置,如果没有,为什么不呢?
解决方法
所以我花了一分钟看了MVC 3的来源.我遇到的第一件事就是这个功能看起来有些笨拙.主要是因为他们重用一个属性,在一种情况下工作,尊重所有属性和配置设置,然后在子操作方案中忽略所有这些设置,只允许VaryByParam和持续时间.
如何找出支持的内容超出了我的范围.因为除非你提供了一个持续时间和一个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);
- }
我不确定为什么在documentation中没有调用它,但即使它是api也应该清楚,或者至少抛出正确的异常.
简而言之,部分输出缓存有效,但不像你想要的那样.我将努力修复代码并尊重某些设置,如启用.
更新:
我修改了当前的实现,至少在我的情况下工作,尊重启用标志并允许来自web.config的缓存配置文件. Detailed in my blog post.