2

I have controller action decorated with OutputCache attribute:

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

I would like to disable it in debug mode so I have used web.config transformation so in DEBUG mode I get this extra lines:

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

But cache still works - action result is cached, changing code inside view make no effect when rendered.

Any ideas?

IT Man

1
  • Wrap your [OutputCache] attribute in #if !DEBUG statement Commented Feb 7, 2019 at 14:03

2 Answers 2

2

You can do:

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

#if (C# Reference)

Sign up to request clarification or add additional context in comments.

1 Comment

Its a bit dirty and decentralized approach, but if there's no other way I will give it a try.
1

You need to use 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>

Transformation at 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>

When publishing it a release mode it will produce this for web.config:

<add name="CacheProfile1" duration="43200" varyByParam="*" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.