1。创建自己的异常处理程序
public class ExceptionHandlerMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<ExceptionHandlerMiddleware> _logger;
public ExceptionHandlerMiddleware(RequestDelegate next, ILogger<ExceptionHandlerMiddleware> logger)
{
_next = next;
_logger = logger;
}
public async Task Invoke(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception exception)
{
HandleException(exception);
throw;
}
}
}
通过抛出,我们传递给系统处理程序
在启动时声明
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseStaticFiles();
app.UseExceptionHandler("/Error");
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseMiddleware<ExceptionHandlerMiddleware>();
无法在/错误上进行重定向,而是会重定向到起始页面。 问题是什么? 3.路线
app.UseMvc(routes => { route.MapRoute( 名称:“ root”, 模板:“”, 默认值:new {area =“ Default”,控制器=“ Home”,action =“ News”} );
routes.MapRoute(
name: "my",
template: "{controller}/{action}",
defaults: new { area = "My" }
);
routes.MapRoute(
name: "test",
template: "{controller}/{action}",
defaults: new { area = "Test" }
);
routes.MapRoute(
name: "myApp",
template: "{controller}/{action}",
defaults: new { area = "MyApp" }
);
routes.MapRoute(
name: "queue",
template: "{controller}/{action}",
defaults: new { area = "Queue" }
);
routes.MapRoute(
name: "areaRoute",
template: "{area:exists}/{controller}/{action}/{id?}",
defaults: new { action = "Index" },
constraints: new { id = new LongRouteConstraint() }
);
routes.MapRoute(
name: "default",
template: "{action}",
defaults: new { area = "Default", controller = "Home" }
);
});
是否可以在不使用UseExceptionHandler的情况下过渡到错误页面?