ASP.NET Core中的全局过滤器顺序

Mar*_*cin 5 c# action-filter asp.net-core

我在Startup中有两个全局异常过滤器

config.Filters.AddService(typeof(ExceptionFilter));
Run Code Online (Sandbox Code Playgroud)

config.Filters.AddService(typeof(JsonExceptionFilter));
Run Code Online (Sandbox Code Playgroud)

我总是觉得如果首先添加相同的过滤器,它首先执行.

但是当我ExceptionFilter第一次添加时,它会被执行第二次.

更新1

ConfigureServices 方法:

services
    .AddMvc(
         config =>
         {
            config.Filters.AddService(typeof(ExceptionFilter));
            config.Filters.AddService(typeof(JsonExceptionFilter));
         });
Run Code Online (Sandbox Code Playgroud)

Joh*_*n H 7

使用MVC,您可以指定一个确定过滤器执行顺序的订单值.我不知道是否同样适用于ASP.NET Core,我没有在我面前检查IDE.你介意试试吗?

config.Filters.AddService(typeof(ExceptionFilter), 2);
config.Filters.AddService(typeof(JsonExceptionFilter), 1);
Run Code Online (Sandbox Code Playgroud)

即使不是这样,检查是否有另一个重载AddService确实接受一个参数来指定顺序.