Controller.OnException 是否在 ExceptionFilter 之前被调用



试图在这里理解MVC管道:

顺序似乎是这样的:

  1. 授权筛选器
  2. 操作执行
  3. 操作执行
  4. 操作已执行
  5. OnResultExecution
  6. 创建操作结果
  7. OnResultExecute
  8. 写入响应流

Controller.OnException 何时相对于 ExceptionFilterAttribute.OnException 运行?

它可能记录在某个地方,至少在源代码中,但我刚刚运行了这个小实验:

// in MyHandleErrorAttribute, globally configured
public override void OnException(ExceptionContext filterContext)
{
    Debug.Print("HandleErrorAttribute.OnException 1");
    base.OnException(filterContext);
    Debug.Print("HandleErrorAttribute.OnException 2");
}
...
// in HomeController
protected override void OnException(ExceptionContext filterContext)
{
    Debug.Print("Controller OnException 1");
    base.OnException(filterContext);
    Debug.Print("Controller OnException 2");
}

和输出窗口显示:

HandleErrorAttribute.OnException 1
HandleErrorAttribute.OnException 2
控制器异常 1
控制器异常 2

最新更新