asp.net-mvc – 在ControllerInstanceFilterProvider的Filters之前运行的GlobalFilterCollection的过滤器

我遇到了一个奇怪的行为,但我不确定我是否在这里正确的轨道.

我有一个控制器,它覆盖了Controller基类的OnException方法.

public class ControllerFiltersController : Controller {

    public ActionResult Index() {

        throw new NotImplementedException();
    }

    protected override void OnException(ExceptionContext filterContext) {

        Trace.Trace@R_677_4045@ion(
            "ControllerFiltersController Exception: " + DateTime.Now.ToString("hh:mm:ss.fff")
        );
    }
}

我还有一个自定义的ExceptionFilter,如下所示:

public class HandleErrorCustom : IExceptionFilter {

    public void OnException(ExceptionContext filterContext) {

        Trace.Trace@R_677_4045@ion(
            "HandleErrorCustom Exception Message: " + DateTime.Now.ToString("hh:mm:ss.fff")
        );
    }
}

然后,我将其注册为全局过滤器:

public static void RegisterGlobalFilters(GlobalFilterCollection filters) {

    filters.Add(new HandleErrorCustom());
}

我期望控制器实例过滤器在全局过滤器之前运行,因为ControllerInstanceFilterProvider提供的过滤器顺序是Int32.MinValue,它们的范围是FilterScope.First.

正如此处所述:ASP.NET MVC 3 Service Location,Part 4: Filters

但结果却不同:

iisexpress.exe @R_677_4045@ion: 0 : HandleErrorCustom Exception Message:
06:56:49.972

iisexpress.exe @R_677_4045@ion: 0 : ControllerFiltersController Exception:
06:56:49.974

这是一个ASP.NET MVC 4应用程序,我不知道任何影响ASP.NET MVC 3的过滤器排序行为的更改.我在这里缺少什么?

解决方法

这是预期的行为.

过滤器排序取决于信息流动的方向.如果信息流入动作,那么订单就像您期望的那样;如果信息流回动作,那么订单将被撤销.

例如,假设您按此顺序有三个过滤器:F1,F2,F3.假设这些是动作过滤器(意思是,它们正在侦听ActionExecuting和ActionExecuted).系统将运行它们的顺序如下:

F1.ActionExecuting()
F2.ActionExecuting()
F3.ActionExecuting()
Action()
F3.ActionExecuted()
F2.ActionExecuted()
F1.ActionExecuted()

根据定义,错误处理程序是在操作的返回端运行的过滤器,因此它们的顺序是相反的.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....