在.net core 3中frombody对象为null

时间:2019-09-24 15:49:02

标签: .net-core-3.0

    [HttpPost("add-car")]
    public async Task<IActionResult> AddCarAsync([FromBody] AddCarInputDtos addCarInputDtos)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
    }

    public class AddCarInputDtos
    {
        public int? CodeParent { get; set; }
        Required(ErrorMessage = "CodeCarList parameter is mandatory.")]
        public int? CodeCarList { get; set; }}
        [EnumDataType(typeof(YesNoEnum), ErrorMessage = "YesNoEnum value doesn't exist within enum")]
        public YesNoEnum? FeatureLoad { get; set; }
    }

当我用application / json格式的邮递员调用此API方法并跟踪代码对象为null时,它返回:

    {
      "$.featureLoad": [
        "The JSON value could not be converted to System.Nullable`1[Jinavat.Backend.Api.Dtos.General.YesNoEnum]. Path: $.featureLoad | LineNumber: 0 | BytePositionInLine: 268."
      ]
    }

请帮助我解决此问题。

1 个答案:

答案 0 :(得分:1)

尝试在ConfigureServices中显式添加NewtonsoftJson:

catch(MyException ex)

根据这些文档:https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#jsonnet-support