If you want set custom exception handling behavior for a specific controller, you can do so by overriding the controllers OnActionExecuted
method.
Remember to set the ExceptionHandled
property to true to disable default exception handling behavior.
Here is a sample from an api I'm writing, where I want to catch specific types of exceptions and return a json formatted result:
private static readonly Type[] API_CATCH_EXCEPTIONS = new Type[] { typeof(InvalidOperationException), typeof(ValidationException) }; public override void OnActionExecuted(ActionExecutedContext context) { base.OnActionExecuted(context); if (context.Exception != null) { var exType = context.Exception.GetType(); if (API_CATCH_EXCEPTIONS.Any(type => exType == type || exType.IsSubclassOf(type))) { context.Result = Problem(detail: context.Exception.Message); context.ExceptionHandled = true; } } }