gpt4 book ai didi

.net-core-3.0 - .net core 3 中的 frombody 对象为 null

转载 作者:行者123 更新时间:2023-12-02 13:13:04 25 4
gpt4 key购买 nike

    [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; }
}

当我使用 postman 以 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."
]
}

请帮我解决这个问题。

最佳答案

尝试在ConfigureServices中显式添加NewtonsoftJson:

    public void ConfigureServices(IServiceCollection services)
{
// services.AddABunchOfStuff();
services.AddNewtonsoftJson();
// services.AddMoreStuff();
}

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

关于.net-core-3.0 - .net core 3 中的 frombody 对象为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58084000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com