如果使用 .net core 3.0 内置的 System.Text.Json ,配置方法如下:
1 services.AddMvc().AddJsonOptions(options => 2 { 3 options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); 4 options.JsonSerializerOptions.PropertyNamingPolicy = null; 5 });
如果使用 NewtonsoftJson ,配置方法如下:
1 services.AddMvc() 2 .AddNewtonsoftJson(options => 3 options.SerializerSettings.ContractResolver = new DefaultContractResolver());
一般使用时两者可以一起配置,配置方法如下:
1 services.AddMvc() 2 .AddJsonOptions(options => 3 { 4 options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); 5 options.JsonSerializerOptions.PropertyNamingPolicy = null; 6 }) 7 .AddNewtonsoftJson(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); });