skip to Main Content

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();
         }

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search