- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 web api 2.0 中有一个全局 ExceptionHandler,它处理所有未处理的异常,以便向 api 调用者返回友好的错误消息。
我还有一个全局 ExceptionFilter,它在我的 web api 中处理一个非常具体的异常并返回一个特定的响应。 ExceptionFilter 是由插件动态添加到我的 web api 中的,因此我无法在我的 ExceptionHandler 中执行它的操作。
我想知道我是否同时全局注册了 ExceptionHandler 和 ExceptionFilter ,哪个将优先并首先执行?现在我可以看到 ExceptionFilter 在 ExceptionHandler 之前执行。而且我还可以在我的 ExceptionFilter 中看到,如果我创建一个响应,则 ExceptionHandler 没有被执行。
可以安全地假设:
最佳答案
我必须通过 System.Web.Http 进行调试才能找到我的问题的答案。所以答案是:
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);
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);
}
关于asp.net-web-api - ASP.NET Web Api 2.0 中哪个优先,ExceptionFilter 还是 ExceptionHandler?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28127042/
我在 .NET 4.6.1 上有一个传统的 WebApi 项目,它有一个全局 ExceptionFilter,它处理已知异常以返回带有相应状态代码的友好消息。 WebApiConfig.cs publ
我在 ASP.NET Core MVC (dnx46) RC1 中有一个带有 AuthorizationHandler 的应用程序: public class AppSumAuthAuthorizat
堆栈, 出于某种原因,我的 Owin WebApi 服务忽略了我们的自定义异常处理程序。我正在关注 asp.net exception handling 的文档.以下是简化的实现细节(删除了业务专有内
我已经编写了一个 ExceptionFilter 属性,其中我需要访问 dbContext 类来处理数据库事务。但是我在我的过滤器属性中收到了 null 引用。 有什么方法可以获得 dbContext
我尝试使用 ExceptionFilter 将异常映射到对应的 HTTP。 这是我的代码: @Catch(EntityNotFoundError) export class EntityNotFoun
我有一个自定义异常 FilterAttribute,如下所示: [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Al
我创建了一个 ExceptionFilter: public class LogException : FilterAttribute, IExceptionFilter { void I
我已将 Serilog 添加到我的项目中: 程序.cs .UseSerilog((provider, context, loggerConfiguration) => { loggerConf
在这里尝试理解 MVC 管道: 顺序好像是这样的: 授权过滤器 OnActionExecuting Action 执行 OnActionExecuted OnResultExecuting 创建操作结
我正在使用 NestJS 5.4.0 我有自定义 LoggerService,它运行良好。但是,如何将此 LoggerService 添加到 ExceptionFilter。 // logger.se
本文转载自微信公众号「神光的编程秘籍」,作者神说要有光zxg。转载本文请联系神光的编程秘籍公众号。 对输入做验证是一个 web 应用的基本功能,不止前端要做、后端也要做:
我在这里实现了异常过滤器:http://www.asp.net/web-api/overview/web-api-routing-and-actions/exception-handling 并在全局
就是这样。 在MVC或WebApi中编写自定义异常过滤器时,OnExceptionAsync和OnException方法有什么区别?是否仅在使用异步 Controller 时才调用 OnExcepti
我正在读这个 article并且似乎有三个不同的异常事件。 我想使用像 Serilog 这样的日志框架来捕获异常。 我应该把日志代码放在 ExceptionFilter 中吗? ,然后 Excepti
我已经创建了一个 Asp.Net Core MVC 应用程序。我想处理两种类型的错误。 我创建了两个异常:UserFriendlyException 和 UserFriendlyViewExcepti
我的 web api 2.0 中有一个全局 ExceptionHandler,它处理所有未处理的异常,以便向 api 调用者返回友好的错误消息。 我还有一个全局 ExceptionFilter,它在我
我是一名优秀的程序员,十分优秀!