0

Is there a way to disable the outputcache here programmatically if something happens that is not an exception?

  [OutputCache(CacheProfile = "StatisticSheets")]
        public virtual ActionResult GameStatistics(int? eventId, int? divisionId, string ids)
        {
            If(true) {
            // Don't Cache This Page
            }
            return View();
         }
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
  • Is this helpful? https://stackoverflow.com/questions/28577455/conditional-cache-using-outputcacheattribute-in-asp-net-mvc-4 – Lajos Arpad Jul 15 '22 at 12:47
  • This is a potential duplicate of https://stackoverflow.com/questions/9380321/how-to-set-remove-attributes-dynamically-in-c – Amogh Sarpotdar Jul 16 '22 at 18:43
  • No has nothing to do with this question @AmoghSarpotdar – Mike Flynn Jul 17 '22 at 13:43
  • @MikeFlynn : Cant you remove the OutputCache attribute programmatically that is shown in the link above? Check this out - https://stackoverflow.com/questions/2483124/remove-c-sharp-attribute-of-a-property-dynamically – Amogh Sarpotdar Jul 17 '22 at 15:43

2 Answers2

0

This is how i have done:

create a derive class from outputcache:

public class MyOutputCache : OutputCacheAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        // checking route data for special condition
        if(!filterContext.RouteData.Values.TryGetValue("abortcaching",out object _))
        {
            base.OnResultExecuting(filterContext);
        }
    }
}

then in controller:

    [MyOutputCache(Duration = 60, VaryByParam = "none")]
    public ActionResult Index()
    {
        ViewBag.Title = "Home Page " + DateTime.Now.Minute.ToString();
        
        // it can be your condition
        if (DateTime.Now.Minute %2 == 0) 
        {
            RouteData.Values.Add("abortcaching", "true");
        }
        return View();
    }

hope it helps.

CodingMytra
  • 2,234
  • 1
  • 9
  • 21
-1

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

Purvesh Sangani
  • 295
  • 1
  • 9