如何在 asp.net 核心 3 中设置 json 序列化器设置?

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

json 遗留 asp.net 核心应用程序的序列化程序设置是通过添加 AddMvc().AddJsonOptions() 设置的,但我没有在 asp.net core 3 中使用 AddMvc()。那么如何设置全局 json 序列化设置?

选项 A. 添加控制器

这仍然是 MVC,需要 Microsoft.AspNetCore.Mvc.NewtonsoftJson nuget 包,但你说你使用 AddControllers

来自Add Newtonsoft.Json-based JSON format support

services.AddControllers().AddNewtonsoftJson(options =>
{
    // Use the default property (Pascal) casing
    options.SerializerSettings.ContractResolver = new DefaultContractResolver();

    // Configure a custom converter
    options.SerializerOptions.Converters.Add(new MyCustomJsonConverter());
});

选项 B. 默认设置

JsonConvert.DefaultSettings = () => new JsonSerializerSettings (...)

JsonConvert.DefaultSettings Property

Gets or sets a function that creates default JsonSerializerSettings. Default settings are automatically used by serialization methods on JsonConvert, and ToObject () and FromObject(Object) on JToken. To serialize without using any default settings create a JsonSerializer with Create().

AddMvc return 是一种 IMvcBuilder implementation, which has a corresponding AddJsonOptions 扩展方法。新式方法 AddControllersAddControllersWithViewsAddRazorPages 也是 return 和 IMvcBuilder 实现。以与 AddMvc:

链接相同的方式链接这些
services.AddControllers()
    .AddJsonOptions(options =>
    {
        // ...
    });

请注意,此处的 options 不再适用于 Json.NET,而是适用于较新的 System.Text.Json API。如果您仍想使用 Json.NET,请参阅

不需要添加 Newtonsoft,在 .Net Core 3.0 项目上添加 Newtonsoft 兼容包是个问题。

另见 https://github.com/aspnet/AspNetCore/issues/13564

当然,人们会庆祝 属性 命名 PascalCase,目前 NA... 所以null for PropertyNamingPolicy表示PascalCase,显然不是很好

// Pascal casing
services.AddControllersWithViews().
        AddJsonOptions(options =>
        {
            options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
            options.JsonSerializerOptions.PropertyNamingPolicy = null;
        });

您可以尝试 System.Text.Json,新发布的 Json nuget 包转换器。 Newtonsoft 在 .Net Core 中不再运行良好。 Startup.cs如下 您可以在 configirationSetting 方法中编写此代码。

 services.AddControllers()
     .AddJsonOptions(options =>
      {
          options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
          options.JsonSerializerOptions.PropertyNamingPolicy = null;
          options.JsonSerializerOptions.Converters.Add (new JsonStringEnumConverter ());
      });  

1.install NuGet: Microsoft.AspNetCore.Mvc.NewtonsoftJson 或

   <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.2" />
    </ItemGroup>

2。添加 Startup.cs :

   public void ConfigureServices(IServiceCollection services)
     {
         //JSON Serializer
         services.AddControllers().AddNewtonsoftJson(options =>
           {
            options.SerializerSettings.ReferenceLoopHandling = 
               Newtonsoft.Json.ReferenceLoopHandling.Ignore;
           });
    }

.net6中在.AddControllers()之后添加这段代码program.cs 文件:

builder.Services.AddControllers().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.PropertyNamingPolicy = null;
});