默认情况下 ASP.NET Core Web API 仅返回 500 状态码,如何返回具体的异常信息?
HTTP/1.1 500 Internal Server Error
Date: Mon, 14 Oct 2019 12:25:15 GMT
Server: Kestrel
Content-Length: 0
用的是 ASP.NET Core 3.0
通过 Opw.HttpExceptions.AspNetCore 搞定了。
1)安装 nuget 包 Opw.HttpExceptions.AspNetCore
<PackageReference Include="Opw.HttpExceptions.AspNetCore" Version="2.3.0" />
2)Startup.ConfigureServices 中添加 AddHttpExceptions
services.AddControllers().AddHttpExceptions();
3)Startup.Configure 中添加 UseHttpExceptions
app.UseHttpExceptions();
示例响应输出
{"type":"error:invalid-cast","title":"InvalidCast","status":500,"detail":"Invalid cast from \u0027System.Int32\u0027 to \u0027Cnblogs.Infrastructure.Primitives.PostConfig\u0027.","instance":"/zzkdocuments"}
发现一个问题,使用 Opw.HttpExceptions.AspNetCore 后,异常不会被记录到日志