且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在ASP.NET Core 3中设置JSON序列化程序设置?

更新时间:2022-12-04 17:00:24

AddMvc 返回一个 IMvcBuilder 实现,它具有相应的 AddJsonOptions 扩展方法。新型方法 AddControllers AddControllersWithViews AddRazorPages 还返回 IMvcBuilder 实现。以与链接 AddMvc 相同的方式链接它们:

AddMvc returns an IMvcBuilder implementation, which has a corresponding AddJsonOptions extension method. The new-style methods AddControllers, AddControllersWithViews, and AddRazorPages also return an IMvcBuilder implementation. Chain with these in the same way you would chain with AddMvc:

services.AddControllers()
    .AddJsonOptions(options =>
    {
        // ...
    });

请注意,此处的个选项 Json.NET,但对于较新的 System.Text.Json API。如果您仍然想使用Json.NET,请参见 tymtam的答案

Note that options here is no longer for Json.NET, but for the newer System.Text.Json APIs. If you still want to use Json.NET, see tymtam's answer