如何在ASP.NET Core 3中设置JSON序列化程序设置? [英] How to set json serializer settings in asp.net core 3?

查看:230
本文介绍了如何在ASP.NET Core 3中设置JSON序列化程序设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

json序列化程序设置,但我不使用 asp.net核心3 中的AddMvc()。那么如何设置全局json序列化设置?

json serializer settings for legacy asp.net core applications were set by adding AddMvc().AddJsonOptions(), but I don't use AddMvc() in asp.net core 3. So how can I set global json serialization settings?

推荐答案

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

这篇关于如何在ASP.NET Core 3中设置JSON序列化程序设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆