gro*_*kky 4 c# asp.net asp.net-core-mvc asp.net-core
我正在使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
与动作过滤器不同,异常过滤器不会让我访问Controller,所以我不能这样做c.Result = controller.RedirectTo...().
那么如何重定向到我的错误操作?
Kev*_*sse 10
该HttpContext暴露的ExceptionContext,所以你可以用它来重定向.
context.HttpContext.Response.Redirect("...");
Run Code Online (Sandbox Code Playgroud)
还有一个Result属性,但我不知道它是否会在执行过滤器后被解释.不过值得一试:
context.Result = new RedirectResult("...");
Run Code Online (Sandbox Code Playgroud)
如果它有效,它也应该与RedirectToActionResult或一起使用RedirectToRouteResult.
| 归档时间: |
|
| 查看次数: |
1858 次 |
| 最近记录: |