If you need global to disable then read the below details, at the end also I shared with action method implementation code.
First, we want to capture whether or not the app is in debugging mode when it is launched. We'll store that in a global variable to keep things speedy.
public static class GlobalVariables
{
public static bool IsDebuggingEnabled = false;
}
Then in the Global.asax code's Application_Start method, write to the global property.
protected void Application_Start()
{
SetGlobalVariables();
}
private void SetGlobalVariables()
{
CompilationSection configSection = (CompilationSection)ConfigurationManager
.GetSection("system.web/compilation");
if (configSection?.Debug == true)
{
GlobalVariables.IsDebuggingEnabled = true;
}
}
Now we will create our own class to use for caching, which will inherit from OutputCacheAttribute
.
public class DynamicOutputCacheAttribute : OutputCacheAttribute
{
public DynamicOutputCacheAttribute()
{
if (GlobalVariables.IsDebuggingEnabled)
{
this.VaryByParam = "*";
this.Duration = 0;
this.NoStore = true;
}
}
}
Now when you decorate your controller endpoints for caching, simply use your new attribute instead of [OutputCache]
.
// you can use CacheProfiles or manually pass in the arguments, it doesn't matter.
// either way, no caching will take place if the app was launched with debugging
[DynamicOutputCache(CacheProfile = "Month")]
public ViewResult contact()
{
return View();
}
With Action Method
-> For .net Framework: [OutputCache(NoStore = true, Duration = 0)]
-> For .net Core: [ResponseCache(NoStore = true, Duration = 0)]
You can use the particular action method above way