这个问题可能是由于在控制器中缺少错误处理程序而引起的。在您的Startup.cs文件的Configure方法中,确保添加以下代码:
app.UseExceptionHandler(errorApp => { errorApp.Run(async context => { context.Response.StatusCode = 500; context.Response.ContentType = "text/html";
await context.Response.WriteAsync("\r\n");
await context.Response.WriteAsync("\r\nERROR
\r\n");
await context.Response.WriteAsync("Details: " + context.Features.Get ().Error.Message + "
\r\n");
await context.Response.WriteAsync("\r\n");
await context.Response.WriteAsync(new string(' ', 512)); // IE padding
});
});
这将为您的应用程序添加一个全局错误处理程序。如果您想在控制器中定义自定义错误处理程序的话,只需在其中包含以下代码:
public class HomeController : Controller
{
[Route("/error")]
public IActionResult Error()
{
var feature = HttpContext.Features.Get
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
在上面的代码中,您可以使用IExceptionHandlerFeature接口中包含的更多错误详细信息,并使用该新的ErrorViewModel创建自己的错误视图的表现形式。