多參數傳值,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'
