我正在使用ASP.NET Core。我的其中一个控制器调用服务,该服务会抛出各种异常。我想在异常过滤器(而非中间件)中处理它们。
public class MyHandlerAttribute : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext c)
{
if (c.Exception is FooException) {
// redirect with arguments to here
}
else if (c.Exception is FooException) {
// redirect with arguments to there
}
else {
// redirect to main error action without arguments, as 500
}
base.OnException(c);
}
}
与操作过滤器不同,异常过滤器无法访问
Controller
,因此我无法执行c.Result = controller.RedirectTo...()
。那么,我该如何重定向到我的错误操作?
context.Exception = null;
,则第一个可以工作。但是这种类型的重定向不允许我指定参数 - 这可能吗? - grokkycontext.ExceptionHandled = true;
。 - juunas