[英]Exception handling ASP .NET Core MVC 6
My present configuration:我现在的配置:
public void ConfigureDevelopment(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
//app.UseDeveloperExceptionPage();
//app.UseDatabaseErrorPage();
app.UseStatusCodePagesWithRedirects("/error/{0}");
app.UseExceptionHandler();
Configure(app);
}
I have this controller that is supposed to be executed when the server register any exceptions.我有这个应该在服务器注册任何异常时执行的控制器。 When I execute "error/test" I'm redirected to "error/500" as expected.
当我执行“错误/测试”时,我按预期重定向到“错误/500”。 If I manually execute "error/exception", I get the server 500 native error, not mine.
如果我手动执行“错误/异常”,我会收到服务器 500 本机错误,而不是我的。
public class ErrorController : Controller
{
[Route("error/404")]
public ActionResult Error404()
{
return View("404");
}
[Route("error/500")]
public ActionResult Error500()
{
return View("500");
}
[Route("error/test")]
public ActionResult Test()
{
return new StatusCodeResult(500);
}
[Route("error/exception")]
public ActionResult Exception()
{
throw new Exception("Should redirect to error/500");
return Content("nope");
}
}
Any idea how to redirect "error/exception" to "error/500" when exception is thrown in actions?知道如何在操作中抛出异常时将“错误/异常”重定向到“错误/500”吗?
Thank you.谢谢你。
Sure, no problem.好没问题。 use
app.UseExceptionHandler("/error/500");
使用
app.UseExceptionHandler("/error/500");
and then this isn't needed any longer:然后这不再需要了:
[Route("error/exception")]
public ActionResult Exception()
{
throw new Exception("Should redirect to error/500");
return Content("nope");
}
I believe you can specify multiple routes on one action so you dont need the "Exception" action, just add it's route to the 500:我相信您可以在一个操作上指定多个路由,因此您不需要“异常”操作,只需将它的路由添加到 500:
public class ErrorController : Controller
{
[Route("error/404")]
public ActionResult Error404()
{
return View("404");
}
[Route("error/500")]
[Route("error/exception")]
public ActionResult Error500()
{
return View("500");
}
[Route("error/test")]
public ActionResult Test()
{
return new StatusCodeResult(500);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.