if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Administration/Error");
app.UseExceptionHandler("/Production/Error");
}
如何为 Razor 页面上的不同区域定义 2 个错误处理页面?
自定义异常处理程序页面的替代方法是向 UseExceptionHandler 提供 lambda。使用 lambda 允许在返回响应之前访问发生错误的请求的路径。
下面是使用 lambda 进行异常处理的示例:
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
var exceptionHandlerPathFeature =
context.Features.Get<IExceptionHandlerPathFeature>();
// Use exceptionHandlerPathFeature to process the exception (for example,
// logging), but do NOT expose sensitive error information directly to
// the client.
if (exceptionHandlerPathFeature.Path.Contains("/Administration/"))
{
context.Response.Redirect("/Administration/Error");
}
if(exceptionHandlerPathFeature.Path.Contains("/Production/"))
{
context.Response.Redirect("/Production/Error");
}
});
});
你可以参考处理 ASP.NET Core 中的错误:异常处理程序 lambda https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-2.2#exception-handler-lambda.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)