.NET Core 2.2 与 5.0 IActionResult "NotFound" 序列化

问题描述

在 .NET Core 2.2 中,像这样在 WebAPI 控制器中返回 NotFound():

[HttpGet]
public IActionResult DoSomething() {
  return NotFound(new IOException("Unable to locate specified resource."));
}

将在响应正文中使用 404 状态代码和 JSON 进行响应,如下所示:

{
   "ClassName":"System.Exception","Message":"Unable to locate specified resource.","Data":null,"InnerException":null,"HelpURL":null,"StackTraceString":"Huge stack trace goes here...","RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":null,"HResult":-2146233088,"Source":"Namespace goes here...","WatsonBuckets":null
}

但是在 .NET Core 5.0 中,完全相同的代码会生成以下 JSON:

{
   "TargetSite":null,"StackTrace":null,"Data":{
      
   },"HelpLink":null,"Source":null,"HResult":-2146232800
}

现在显然 ObjectResult 的 JSON 序列化在版本之间发生了变化,但没有抛出真正的异常并使用单独的错误处理程序手动序列化它,有没有办法配置序列化以便 NotFound() (或任何其他 ObjectResult问题)会像以前一样返回完整的异常对象吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...