I have two global exception filters in Startup
config.Filters.AddService(typeof(ExceptionFilter));
and
config.Filters.AddService(typeof(JsonExceptionFilter));
I was always under impression that if the same filter is added first it is executed first.
But when I've added ExceptionFilter
first it is executed second.
UPDATE 1
ConfigureServices
method:
services
.AddMvc(
config =>
{
config.Filters.AddService(typeof(ExceptionFilter));
config.Filters.AddService(typeof(JsonExceptionFilter));
});
Filters run in the following order: Authorization filters. Action filters. Response filters.
as you can see from the below diagram, as soon as the controller starts execution through Action Invoker, Authentication and authorization filters are the very first filters to be triggered, followed by model binding which maps request and route data to action parameters.
Filters get executed in the following order for an action: Globally Defined Filters -> Controller-specific Filters -> Action-specific Filters.
With MVC, you can specify an order value that determines the order of execution for your filters. I don't know if the same applies to ASP.NET Core, and I don't have an IDE in front of me to check. Would you mind trying this?
config.Filters.AddService(typeof(ExceptionFilter), 2);
config.Filters.AddService(typeof(JsonExceptionFilter), 1);
Even if that isn't it, check to see if there is another overload of AddService
that does accept a parameter to specify the order.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With