【问题标题】:Does Controller.OnException get called before ExceptionFilter?Controller.OnException 在 ExceptionFilter 之前被调用吗?
【发布时间】:2015-04-27 01:29:19
【问题描述】:

在这里尝试理解 MVC 管道:

好像顺序是这样的:

  1. 授权过滤器
  2. OnActionExecuting
  3. ActionExecutes
  4. OnActionExecuted
  5. OnResultExecuting
  6. 创建动作结果
  7. OnResultExecuted
  8. 写入响应流

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

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

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

    // 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
    控制器 OnException 1
    控制器 OnException 2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-14
      • 2016-10-24
      • 2016-04-06
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多