在基于Razor页面的应用程序中,有没有一种方法可以配置全局异常处理和状态代码页的使用,因此,如果全局异常处理程序将HttpResponse状态设置为给定值,则相关的页面将调用状态代码页的已配置位置?
在Startup类的Configure
方法中,我尝试了以下各种排序。我还尝试了ExceptionFilter,但似乎无法在ExceptionHandler中设置错误代码,并且无法呈现正确的状态页。
app.UseStatusCodePagesWithReExecute("/ErrorPages/{0}");
app.UseExceptionHandler(
options => {
options.Run(
async context =>
{
var ex = context.Features.Get<IExceptionHandlerFeature>();
if (ex != null)
{
// Following simply returns default browser pages for given status
// codes, rather than /ErrorPages/403 or /ErrorPages/404
var exceptionType = ex.Error.GetType();
if (exceptionType == typeof(ForbiddenException))
{
context.Response.StatusCode = (int) HttpStatusCode.Forbidden;
}
else if (exceptionType == typeof(ItemNotFoundException))
{
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
}
await context.Response.WriteAsync(string.Empty).ConfigureAwait(false);
}
});
}
);