多参数传值,string为null解决
1、添加类
public class CustomMetadataProvider : IMetadataDetailsProvider, IDisplayMetadataProvider { public void CreateDisplayMetadata(DisplayMetadataProviderContext context) { if (context.Key.MetadataKind == ModelMetadataKind.Property || context.Key.MetadataKind == ModelMetadataKind.Parameter || context.Key.MetadataKind == ModelMetadataKind.Type) { context.DisplayMetadata.ConvertEmptyStringToNull = false; } } }
2、注册类
services.AddControllersWithViews(options => options.ModelMetadataDetailsProviders.Add(new CustomMetadataProvider()))
对象传值,string为null
1、修改3.1默认的json
//services.AddMvc().AddJsonOptions(options => //{ // options.JsonSerializerOptions.PropertyNamingPolicy = null; //}); services.AddMvc(options => { options.EnableEndpointRouting = false; }).SetCompatibilityVersion(CompatibilityVersion.Version_3_0) .AddNewtonsoftJson(options => {
// 自定义的返回处理 options.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat; options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Include; options.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Include; options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver(); options.SerializerSettings.ConstructorHandling = Newtonsoft.Json.ConstructorHandling.Default; options.SerializerSettings.StringEscapeHandling = Newtonsoft.Json.StringEscapeHandling.Default; options.SerializerSettings.FloatParseHandling = Newtonsoft.Json.FloatParseHandling.Double; options.SerializerSettings.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace; options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; options.SerializerSettings.Converters.Add(new EmptyStringToNullJsonConverter()); });
2、使用JObject接收数据
public string UpdateCompany([FromBody] JObject value) { string jsonStr = JsonConvert.SerializeObject(value); CompanyInfo rcompany = JsonConvert.DeserializeObject<CompanyInfo>(jsonStr); }
3、关于接收的对象为null问题
关键在于
data: JSON.stringify(paras), contentType: 'application/json'