将 Asp.Net Core 2.2 App 迁移到 3.1 时的整数序列化

Integer serialization while migrating Asp.Net Core 2.2 App to 3.1

我正在将 Asp.Net Core 2.2 Web Api 迁移到 3.1

我注意到现在 System.Int32 被 System.Text.Json

序列化为字符串

在 (2.2) 之前:

{id: 1, ..}

(3.1) 之后:

{id: "1", ..}

编辑 在 C#

中,id 是一个 int 属性

因此需要更改使用 Web api 的 Js - 相当繁重的迁移,我并不急于这样做。

我找到了以下选项:

  1. Keeping NewtonSoft Json.Net for serialization

    这是一个解决方案。但我想了解最新的技术!

  2. Providing a custom converter for integers

    我的情况似乎不起作用

有没有办法告诉 System.Text.Json 中的新 Json 序列化程序将整数序列化为 4​​2 而不是“42”?

阅读 Hostel 的建议并从新 Web Api ASP.NET Core 3.1 应用程序检查并看到预期行为后,我比较了我的 Startup.cs 到 Visual Studio 模板应用之一。

我刚刚在Startup.ConfigureServices

中添加了
services.AddControllers();

尽管控制器缺少缺少的服务 "worked",但并不像预期的那样。

可能是因为存在另一种添加了其他服务的扩展方法。

感谢大家的仔细阅读、评论和提问