在 ASP.NET Core 的 Error action 中可以通过异常处理程序提供的 ExceptionContext 对象来获取产生异常的 ControllerContext 实例。具体步骤如下:
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
var exceptionHandlerPathFeature = context.Features.Get();
var controllerContext = exceptionHandlerPathFeature?.Path;
// 在这里处理异常并返回错误页面
});
});
public IActionResult Error()
{
var controllerContext = HttpContext.Features.Get()?.Path;
// 在这里处理异常并返回错误页面
}
通过以上步骤可以在 ASP.NET Core 的 Error action 中获取产生异常的 ControllerContext 实例。