我希望将具有4XX或5XX错误代码的HTTP请求重定向到自定义错误页面,同时将错误代码保持在请求级别。我还想将异常重定向到错误代码为500的自定义错误页面。
对于我在启动文件中使用的 “ app.UseStatusCodePagesWithReExecute(” / error / {0}“)); app.UseExceptionHandler(“ / error / 500”);“ 与错误控制器关联。
关于异常的部分效果很好。 我还设法将不存在的路由重定向到我的自定义页面,同时保持404错误。
但是,我无法将以下操作重定向到我的自定义错误页面:
- 返回NotFound()
- 返回BadRequest()
- 返回StatusCode(404)
将采用什么技术解决方案来实现这一目标?
这是我的启动文件的配置功能:
app.UseStatusCodePagesWithReExecute("/error/{0}");
app.UseExceptionHandler("/error/500");
app.UseHttpsRedirection();
app.UseStaticfiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "Error-StatusCode-Route",pattern: "error/{statusCode}",defaults: new { controller = "Error",action = "InternalServerError" }
);
endpoints.MapControllerRoute(
name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
});