【问题标题】:MVC disable cache for controller action in debug modeMVC 在调试模式下禁用控制器操作的缓存
【发布时间】:2019-07-01 16:07:04
【问题描述】:

我的控制器动作用 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 人

【问题讨论】:

  • [OutputCache] 属性包装在#if !DEBUG 语句中

标签: c# asp.net-mvc


【解决方案1】:

你可以这样做:

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

#if (C# Reference)

【讨论】:

  • 它有点肮脏和分散的方法,但如果没有其他方法我会尝试一下。
【解决方案2】:

你需要使用CacheProfile:

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

web.config:

<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="*" />

【讨论】:

    猜你喜欢
    • 2014-07-19
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 2017-09-01
    • 2011-01-08
    • 2017-11-22
    相关资源
    最近更新 更多