John Zumbrum
John Zumbrum

Reputation: 2856

Does Controller.OnException get called before ExceptionFilter?

Trying to understand the MVC pipeline here:

It seems that the order is like so:

  1. AuthorizationFilters
  2. OnActionExecuting
  3. ActionExecutes
  4. OnActionExecuted
  5. OnResultExecuting
  6. Create the action result
  7. OnResultExecuted
  8. Write to response stream

When does the Controller.OnException run relative to the ExceptionFilterAttribute.OnException?

Upvotes: 4

Views: 380

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273244

It's probably documented somewhere, in the source at least, but I just ran this little experiment:

// 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");
}

and the Output Window shows:

HandleErrorAttribute.OnException 1
HandleErrorAttribute.OnException 2
Controller OnException 1
Controller OnException 2

Upvotes: 3

Related Questions