In an ASP.NET Core 2.0 application, I am trying to execute a global filter OnActionExecutingbefore executing a controller variant. The expected behavior is that I can prepare something in the global before and pass the value of the result to the controller. However, the current behavior is that the execution order is drawn by design.
The docs tell me about the default execution order :
Each controller that inherits the base class of the controller includes the OnActionExecuting and OnActionExecuted methods. These methods wrap the filters that run for this action: OnActionExecuting is called before any of the filters, and OnActionExecuted is called after all the filters.
This makes me interpret that Controller OnActionExecutingis running before any of the filters. Has the meaning. But the docs also indicate that the default order can be overridden by implementation IOrderedFilter.
My attempt to implement this in a filter looks like this:
public class FooActionFilter : IActionFilter, IOrderedFilter
{
public int Order => 0;
public void OnActionExecuting(ActionExecutingContext context)
{
var foo = "bar";
context.RouteData.Values.Add("foo", foo);
}
}
This filter is registered at startup as:
services.AddMvc(options => options.Filters.Add(new FooActionFilter()));
Finally, my BaseController looks like a sample below. This best explains what I'm trying to achieve:
public class BaseController : Controller
{
public override void OnActionExecuting(ActionExecutingContext context)
{
var foo = context.RouteData.Values.GetValueOrDefault("foo").ToString();
}
}
Order 0 , -1, , .
: , OnActionExecuting () OnActionExecuting?