我必须通过 System.Web.Http 进行调试才能找到我的问题的答案。所以答案是:
可以安全地假设 ExceptionFilters 将在 ExceptionHandlers 之前执行
如果 ExceptionFilter 创建响应,则不会执行 ExceptionHandler。
为什么会这样:
当您注册了一个 ExceptionFilter 以全局执行或为您的控制器操作执行时,所有 api 控制器继承的 ApiController 基类会将结果包装在 ExceptionFilterResult 中并调用其 ExecuteAsync 方法。这是 ApiController 中的代码,它执行以下操作:
if (exceptionFilters.Length > 0)
{
IExceptionLogger exceptionLogger = ExceptionServices.GetLogger(controllerServices);
IExceptionHandler exceptionHandler = ExceptionServices.GetHandler(controllerServices);
result = new ExceptionFilterResult(ActionContext, exceptionFilters, exceptionLogger, exceptionHandler,
result);
}
return result.ExecuteAsync(cancellationToken);
查看 ExceptionFilterResult.ExecuteAsync 方法:
try
{
return await _innerResult.ExecuteAsync(cancellationToken);
}
catch (Exception e)
{
exceptionInfo = ExceptionDispatchInfo.Capture(e);
}
// This code path only runs if the task is faulted with an exception
Exception exception = exceptionInfo.SourceException;
Debug.Assert(exception != null);
bool isCancellationException = exception is OperationCanceledException;
ExceptionContext exceptionContext = new ExceptionContext(
exception,
ExceptionCatchBlocks.IExceptionFilter,
_context);
if (!isCancellationException)
{
// We don't log cancellation exceptions because it doesn't represent an error.
await _exceptionLogger.LogAsync(exceptionContext, cancellationToken);
}
HttpActionExecutedContext executedContext = new HttpActionExecutedContext(_context, exception);
// Note: exception filters need to be scheduled in the reverse order so that
// the more specific filter (e.g. Action) executes before the less specific ones (e.g. Global)
for (int i = _filters.Length - 1; i >= 0; i--)
{
IExceptionFilter exceptionFilter = _filters[i];
await exceptionFilter.ExecuteExceptionFilterAsync(executedContext, cancellationToken);
}
if (executedContext.Response == null && !isCancellationException)
{
// We don't log cancellation exceptions because it doesn't represent an error.
executedContext.Response = await _exceptionHandler.HandleAsync(exceptionContext, cancellationToken);
}
可以看到先执行了ExceptionLogger,然后执行了所有的ExceptionFilter,如果executeContext.Response == null,则执行ExceptionHandler。
我希望这是有用的!