【发布时间】:2021-05-25 14:07:55
【问题描述】:
我使用Hellang 中间件作为我的 MVC 应用程序中的全局异常处理机制进行异常处理。 我在 Startup.cs 的 ConfigureServices 方法中添加了以下代码:
services.AddProblemDetails(opts =>
{
// Control when an exception is included
opts.IncludeExceptionDetails = (ctx, ex) =>
{
// Fetch services from HttpContext.RequestServices
var env = ctx.RequestServices.GetRequiredService<IHostEnvironment>();
return env.IsDevelopment() || env.IsStaging();
};
opts.ShouldLogUnhandledException = (ctx, e, d) =>
{
return (d.Status.HasValue && d.Status.Value >= 500);
};
});
我还在 Configure 方法中添加了UseProblemDetails()。
但是我知道如果我使用UseProblemDetails(),那么UseExceptionHandler() 将不起作用!
因此,我无法找出将用户导航到常见错误视图页面的方法。
在坚持使用 Hellang Middleware 进行异常处理和日志记录的同时,有什么方法可以将用户重定向到错误页面?
【问题讨论】:
标签: exception asp.net-core-mvc middleware