60 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			60 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C#
		
	
	
| 
 | |
| 
 | |
| using Microsoft.Extensions.DependencyInjection;
 | |
| using Newtonsoft.Json;
 | |
| using Newtonsoft.Json.Converters;
 | |
| using System;
 | |
| 
 | |
| namespace IRaCIS.Core.SCP
 | |
| {
 | |
|     public static class NewtonsoftJsonSetup
 | |
|     {
 | |
|         public static void AddNewtonsoftJsonSetup(this IMvcBuilder builder, IServiceCollection services)
 | |
|         {
 | |
|             services.AddHttpContextAccessor();
 | |
|             services.AddScoped<IOSSService,OSSService>();
 | |
| 
 | |
|             builder.AddNewtonsoftJson(options =>
 | |
|              {
 | |
|                  //options.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
 | |
|                  // 忽略循环引用
 | |
|                  options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
 | |
|                  //options.SerializerSettings.TypeNameHandling = TypeNameHandling.All;
 | |
| 
 | |
|                  //处理返回给前端  可空类型 给出默认值 比如in? 为null 设置 默认值0
 | |
|                  options.SerializerSettings.ContractResolver = new NullToEmptyStringResolver(); //new DefaultContractResolver();// new NullToEmptyStringResolver();
 | |
|                                                                                                 // 设置时间格式
 | |
|                  options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
 | |
| 
 | |
|                  options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;
 | |
| 
 | |
|                  //options.SerializerSettings.Converters.Add(new JSONCustomDateConverter()) ;
 | |
| 
 | |
|                  //options.SerializerSettings.Converters.Add(services.BuildServiceProvider().GetService<JSONTimeZoneConverter>());
 | |
| 
 | |
| 
 | |
| 
 | |
|              })
 | |
|                 .AddControllersAsServices()//动态webApi属性注入需要
 | |
|                .ConfigureApiBehaviorOptions(o =>
 | |
|                 {
 | |
|                     o.SuppressModelStateInvalidFilter = true; //自己写验证
 | |
|                  
 | |
|                 });
 | |
| 
 | |
| 
 | |
|             Newtonsoft.Json.JsonSerializerSettings setting = new Newtonsoft.Json.JsonSerializerSettings();
 | |
|             JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() =>
 | |
|             {
 | |
|                 //日期类型默认格式化处理
 | |
|                 setting.DateFormatString = "yyyy-MM-dd HH:mm:ss";
 | |
|                 setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
 | |
| 
 | |
|                 return setting;
 | |
|             });
 | |
| 
 | |
| 
 | |
|         }
 | |
|     }
 | |
| }
 |