.net core 3.1 The JSON value could not be converted to 使用NewtonsoftJson序列化入参

.net core 3.1已经将默认的入参序列化由NewtonsoftJson改为System.Text.Json, 但是这个东西不好用, 例如某个值填空可能就报错

 我们可以将入参序列化改为NewtonsoftJson

1. nuget 安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson

 2. 在startup.cs里的ConfigureServices添加代码

public void ConfigureServices(IServiceCollection services)
{
     services.AddControllers().AddNewtonsoftJson(); 
}

3. 如果需要格式化出参的时间格式

public void ConfigureServices(IServiceCollection services)
{
     services.AddControllers().AddNewtonsoftJson((option) =>
     {
            option.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";//时间格式化
      }); 
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章