How to set json serializer settings in asp.net core 3?

后端 未结 3 1011
执笔经年
执笔经年 2020-12-05 22:42

json serializer settings for legacy asp.net core applications were set by adding AddMvc().AddJsonOptions(), but I don\'t use AddMvc() in asp.

3条回答
  •  自闭症患者
    2020-12-05 23:12

    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 =>
        {
            // ...
        });
    

    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

提交回复
热议问题