2

我有用 OutputCache 属性装饰的控制器操作:

    [OutputCache(Duration = 60 * 60 * 12, VaryByParam = "*")]
    public ActionResult GetProducts(int id, string template, string version)

我想在调试模式下禁用它,所以我使用了 web.config 转换,所以在调试模式下我得到了这些额外的行:

<caching>
  <outputCache enableOutputCache="false" enableFragmentCache="false" />
</caching>

但是缓存仍然有效 - 动作结果被缓存,在视图中更改代码在渲染时无效。

有任何想法吗?

IT人

4

2 回答 2

1

您需要使用CacheProfile

[OutputCache(CacheProfile = "CacheProfile1")]
public ActionResult GetProducts(int id, string template, string version)

网络配置:

<system.web>
  <caching>
    <outputCacheSettings>
      <outputCacheProfiles>
        <add name="CacheProfile1" duration="0" varyByParam="*" />
      </outputCacheProfiles>
    </outputCacheSettings>
  </caching>
</system.web>

web.Release.config 中的转换:

<system.web>  
  <caching>
    <outputCacheSettings>
      <outputCacheProfiles>
        <add name="CacheProfile1" duration="43200" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
      </outputCacheProfiles>
    </outputCacheSettings>
  </caching>
</system.web>

当以发布模式发布它时,它将为 web.config 生成:

<add name="CacheProfile1" duration="43200" varyByParam="*" />
于 2019-02-07T14:29:40.623 回答
1

你可以做:

#if (!DEBUG)
[OutputCache(Duration = 60 * 60 * 12, VaryByParam = "*")]
#endif

#if(C# 参考)

于 2019-02-07T14:05:42.323 回答