Exception 在.Net核心MVC中使用Hellang中间件进行异常处理
我在MVC应用程序中使用了异常处理中间件作为全局异常处理机制。 我在Startup.cs中的ConfigureServices方法中添加了以下代码:Exception 在.Net核心MVC中使用Hellang中间件进行异常处理,exception,asp.net-core-mvc,middleware,Exception,Asp.net Core Mvc,Middleware,我在MVC应用程序中使用了异常处理中间件作为全局异常处理机制。 我在Startup.cs中的ConfigureServices方法中添加了以下代码: services.AddProblemDetails(opts => { // Control when an exception is included opts.IncludeExceptionDetails = (ctx, ex) =>
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);
};
});
services.AddProblemDetails(选项=>
{
//控制何时包含异常
opts.IncludeExceptionDetails=(ctx,ex)=>
{
//从HttpContext.RequestServices获取服务
var env=ctx.RequestServices.GetRequiredService();
return env.IsDevelopment()| | env.IsStaging();
};
opts.ShouldLogUnhandledException=(ctx,e,d)=>
{
返回(d.Status.HasValue&&d.Status.Value>=500);
};
});
我还在Configure方法中添加了UseProblemDetails()
然而,我知道如果我使用UseProblemDetails()
,那么UseExceptionHandler()
将无法工作!
因此,我无法找到一种将用户导航到常见错误视图页面的方法
在坚持使用Hellang中间件进行异常处理和日志记录时,有没有办法将用户重定向到错误页面?请参见此处的答案:
您必须区分请求的类型,无论是API请求还是UI请求,以分别确定是应返回问题+详细信息JSON还是应返回网页
这是我在Startup.cs的Configure
方法顶部附近所做的操作:
app.UseWhen(context => context.IsApiRequest(), branch =>
{
branch.UseProblemDetails();
});
app.UseWhen(context => !context.IsApiRequest(), branch =>
{
branch.UseExceptionHandler("/Home/Error");
});
您可以定义自己的自定义HttpContext扩展方法:
public static class HttpContextExtensions
{
public static bool IsApiRequest(this HttpContext context)
{
return context.Request.Path.StartsWithSegments("/api", StringComparison.OrdinalIgnoreCase)
|| (context.Request.Headers["X-Requested-With"] == "XMLHttpRequest"); // AJAX request
}
}
这并不是你问题的答案,但我在一个Web API应用程序中遇到了一个类似的问题,关于使用ExceptionHandler中间件和Hellang ProblemDetails中间件,我也意识到我不能同时使用这两个中间件,因为它们都以各自的方式更改响应,并相互影响
根据文档,您可以使用ProblemDetails包的一个配置选项在更改响应之前执行代码,并在那里记录所需的所有信息
services.AddProblemDetails(options =>
{
options.IncludeExceptionDetails = (context, ex) =>
{
var environment = context.RequestServices.GetRequiredService<IWebHostEnvironment>();
return environment.IsDevelopment();
};
options.Map<IdentityException>(exception => new ProblemDetails()
{
Title = exception.Title,
Detail = exception.Detail,
Status = StatusCodes.Status500InternalServerError,
Type = exception.Type,
Instance = exception.ToString()
});
options.OnBeforeWriteDetails = (ctx, pr) =>
{
//here you can do the logging
logger.LogError("Exception Occurred!!!!");
logger.LogError(pr.Detail);
logger.LogError(pr.Instance);
};
});
services.AddProblemDetails(选项=>
{
options.IncludeExceptionDetails=(上下文,例如)=>
{
var environment=context.RequestServices.GetRequiredService();
return-environment.IsDevelopment();
};
options.Map(异常=>newproblemdetails()
{
Title=例外。Title,
Detail=exception.Detail,
状态=状态代码。状态500InternalServerError,
Type=exception.Type,
Instance=exception.ToString()
});
options.OnBeforeWriteDetails=(ctx,pr)=>
{
//您可以在这里进行日志记录
logger.LogError(“发生异常!!!!”);
logger.LogError(请购单详情);
logger.LogError(pr.Instance);
};
});
在这里,我使用一个自定义异常和响应中问题详细信息对象所需的额外字段,并使用实例字段保存异常并记录它