#region 指定错误处理动作
app.UseStatusCodePagesWithReExecute("/Error/{0}");//只要不是200 都能进来
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
context.Response.StatusCode = 200;
context.Response.ContentType = "text/html";

await context.Response.WriteAsync("<html lang=\"en\"><body>\r\n");
await context.Response.WriteAsync("ERROR!<br><br>\r\n");

var exceptionHandlerPathFeature =
context.Features.Get<IExceptionHandlerPathFeature>();

Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
Console.WriteLine($"{exceptionHandlerPathFeature?.Error.Message}");
Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");

// Use exceptionHandlerPathFeature to process the exception (for example,
// logging), but do NOT expose sensitive error information directly to
// the client.

if (exceptionHandlerPathFeature?.Error is FileNotFoundException)
{
await context.Response.WriteAsync("File error thrown!<br><br>\r\n");
}

await context.Response.WriteAsync("<a href=\"/\">Home</a><br>\r\n");
await context.Response.WriteAsync("</body></html>\r\n");
await context.Response.WriteAsync(new string(' ', 512)); // IE padding
});
});
app.UseHsts();
#endregion

相关文章:

  • 2022-12-23
  • 2021-12-09
  • 2021-06-10
  • 2022-12-23
  • 2021-04-22
  • 2021-05-13
  • 2021-08-13
猜你喜欢
  • 2022-12-23
  • 2021-05-02
  • 2022-02-14
  • 2021-12-06
相关资源
相似解决方案