title: Execute global filter before controller's OnActionExecuting, in ASP.NET Core
body: <p>In an ASP.NET Core 2.0 application, I am trying to execute a global filter's <code>OnActionExecuting</code> <em>before</em> executing the Controller's variant. Expected behaviour is that I can prepare something in the global before and pass along the result value to the controller(s). The current behaviour, however, is that the order of execution is reversed by design.</p>
  
  <p>The docs tell me about the <a href="https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-2.0#default-order-of-execution" rel="noreferrer">default order of execution</a>: </p>
  
  <blockquote>
    <p>Every controller that inherits from the Controller base class includes OnActionExecuting and OnActionExecuted methods. These methods wrap the filters that run for a given action: OnActionExecuting is called before any of the filters, and OnActionExecuted is called after all of the filters.</p>
  </blockquote>
  
  <p>Which leads me to interpret that the Controller's <code>OnActionExecuting</code> is executed before any of the filters. Makes sense. But the docs also states that <a href="https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-2.0#overriding-the-default-order" rel="noreferrer">the default order can be overridden</a> by implementing <code>IOrderedFilter</code>.</p>
  
  <p>My attempt to implement this in a filter is like so:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class FooActionFilter : IActionFilter, IOrderedFilter
  {
      // Setting the order to 0, using IOrderedFilter, to attempt executing
      // this filter *before* the BaseController's OnActionExecuting.
      public int Order =&gt; 0;
  
      public void OnActionExecuting(ActionExecutingContext context)
      {
          // removed logic for brevity
          var foo = "bar";
  
          // Pass the extracted value back to the controller
          context.RouteData.Values.Add("foo", foo);
      }
  }
  </code></pre>
  
  <p>This filter is registered at startup as:</p>
  
  <pre class="lang-cs prettyprint-override"><code>services.AddMvc(options =&gt; options.Filters.Add(new FooActionFilter()));
  </code></pre>
  
  <p>Finally, my BaseController looks like the sample below. This best explains what I'm trying to achieve:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class BaseController : Controller
  {
      public override void OnActionExecuting(ActionExecutingContext context)
      {
          // The problem: this gets executed *before* the global filter.
          // I actually want the FooActionFilter to prepare this value for me.
          var foo = context.RouteData.Values.GetValueOrDefault("foo").ToString();
      }
  }
  </code></pre>
  
  <p>Setting the <code>Order</code> to 0, or even a non-zero value like -1, does not seem to have any effect on the order of execution.</p>
  
  <p>My question: what can I do to make my global filter execute the <code>OnActionExecuting</code> <em>before</em> the (Base)Controller's <code>OnActionExecuting</code>?</p>