Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of global filters in ASP.NET Core

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));
         });
like image 971
Marcin Avatar asked May 10 '17 08:05

Marcin


People also ask

What is the order in which the action filters are executed?

Filters run in the following order: Authorization filters. Action filters. Response filters.

Which filter is executed first in asp net?

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.

What is the order of the filters that get executed if multiple filters are implemented?

Filters get executed in the following order for an action: Globally Defined Filters -> Controller-specific Filters -> Action-specific Filters.


1 Answers

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.

like image 138
John H Avatar answered Nov 13 '22 16:11

John H