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

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

134246 次浏览

Option A. AddControllers

This is still MVC, and requires Microsoft.AspNetCore.Mvc.NewtonsoftJson nuget package, but you said you use AddControllers.

From 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());
});

Option B. DefaultSettings

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 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

Adding Newtonsoft is not necessary, quite a problems with adding Newtonsoft compatibility packages on .Net Core 3.0 project.

See also https://github.com/aspnet/AspNetCore/issues/13564

Of course, one would celebrate property naming PascalCase, NA at the moment... So null for PropertyNamingPolicy means PascalCase, which is obviously not very good.

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

You can try System.Text.Json, the newly released Json nuget package converter. Newtonsoft no longer works very well in .Net Core. Startup.cs as below You can write this code inside the configirationSetting method.

 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 or

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

2. Add Startup.cs :

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

In .net6 add this code after .AddControllers() in program.cs file:

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