我正在使用ExceptionHandlerAttribute
来处理Web Api 2中的异常。
public class ExceptionHandlerAttribute : ExceptionFilterAttribute
{
public ExceptionHandlerAttribute()
{
}
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
HandleException(actionExecutedContext);
}
private void HandleException(HttpActionExecutedContext actionExecutedContext)
{
// Log exception here
}
}
在其中一个控制器中我有一个异步方法抛出异常(我知道为什么这个方法抛出异常,但这里没有问题)
public async Task<IEnumerable<OrderDTO>> GetOrders(FilterDTO filters)
{
return await _dbcontext.Orders
.Include(x => x.User)
.Where(x => x.OrderDate > filters.StartOrderDate && x.IsApproved)
.Select(x => new OrderDTO()
{
OrderID = x.OrderID,
Amount = x.Amount,
OrderDate = x.OrderDate,
LastModifiedUser = x.User.ToDto()
})
.ToListAsync();
}
当上述方法发生异常时,Web Api框架会按预期调用已注册OnException()
的{{1}}方法。但是在过滤器中,我得到ExceptionHandlerAttribute
method not found error
方法是同一个类中的私有方法,我不确定为什么它找不到方法?
更新1(堆栈跟踪)
at XXXXXXXXX.Api.Filters.ExceptionHandlerAttribute.HandleException(HttpActionExecutedContext actionExecutedContext个)\ r \ n
在XXXXXXXXXX.Api.Filters.ExceptionHandlerAttribute.OnException(HttpActionExecutedContext) actionExecutedContext)in C:\回购\ XXXXXXXXXXXX的\ src \阿比\过滤器\ ExceptionHandlerAttribute.cs:行 30 \ r \ n at System.Web.Http.Filters.ExceptionFilterAttribute.OnExceptionAsync(HttpActionExecutedContext actionExecutedContext,CancellationToken cancellationToken)