Global filter order in ASP.NET Core

I have two Global Exception Filters in Startup

config.Filters.AddService(typeof(ExceptionFilter));

      

and

config.Filters.AddService(typeof(JsonExceptionFilter));

      

It always seemed to me that if the same filter is added first, it is executed first.

But when I added ExceptionFilter

, the second is executed first.

UPDATE 1

ConfigureServices

:

services
    .AddMvc(
         config =>
         {
            config.Filters.AddService(typeof(ExceptionFilter));
            config.Filters.AddService(typeof(JsonExceptionFilter));
         });

      

+3


source to share


1 answer


With MVC, you can specify an order value that determines the execution order 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 test. Could you try to do this?

config.Filters.AddService(typeof(ExceptionFilter), 2);
config.Filters.AddService(typeof(JsonExceptionFilter), 1);

      



Even if it isn't, check if there is another overload AddService

that takes a parameter to specify the order.

+4


source







All Articles