asp.net-web-api – 哪个优先级,ASP.NET Web Api 2.0中的ExceptionFilter
|
我的web api 2.0中有一个全局ExceptionHandler,它处理所有未处理的异常,以便向api调用者返回一个友好的错误消息.
我想知道我是否在全球注册了ExceptionHandler和ExceptionFilter,哪一个将优先执行,首先执行?现在我可以看到ExceptionFilter正在执行ExceptionHandler之前.我也可以看到,在我的ExceptionFilter中,如果我创建一个响应,ExceptionHandler没有被执行. 假设这样做是安全的: > ExceptionFilter在ExceptionHandler之前执行. 解决方法我不得不通过System.Web.Http进行调试,以便找到我的问题的答案.所以答案是:>可以安全地假设ExceptionFilters将在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,然后如果executionContext.Response == null,则执行ExceptionHandler. 我希望这是有用的! (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ASP.NET C#SignalR流到客户端
- asp.net-mvc – Rotativa和Bootstrap网格样式
- asp.net – 使用jquery调用ascx页面方法
- asp.net-web-api – Asp.net WebApi将UTC时间字符串反序列化
- asp.net – 将自定义类添加到HttpContext
- asp.net-mvc – 对每个动作调用使用MVC Miniprofiler
- asp.net – Elmah错误日志记录,我可以只记录一条消息吗?
- .net – 为什么使用DirectoryEntry对LDAP进行身份验证会间歇
- asp.net-mvc – mvc4future无法在ASP.NET MVC 5中使用
- asp.net-mvc – 在ASP.NET MVC控制器结果中设置HTTP状态不呈
- asp.net-mvc – ASP.NET MVC 3自定义授权
- asp.net-core – 是否可以直接在Azure WebApps中
- asp.net-mvc-3 – mvc3 – 如何从源代码禁用符号
- asp.net-mvc – asp.net mvc 3 – ajax表单提交和
- asp.net – CORS错误 – 请求的资源上没有“Acce
- asp.net-mvc-3 – 具有多个强类型部分视图的MVC
- 为什么我的IIS7应用程序池在从ASP.NET页面调用的
- 如何使用ASP.NET MVC项目启动Azure模拟器
- asp.net – 什么是SNIReadSyncOverAsync,为什么需
- asp.net-mvc-3 – 在ASP.NET MVC3中的自定义授权
