无法 post json 反对 asp.net webapi

Not able to post json object to asp.net webapi

我需要将以下 json 对象传递到我的 asp.net core 3.1 web api

{
    "client": {
        "clientID": "3529",
        "clientData": "This a test data"
    },
    "selectedUserIDs": [
        3549,
        3567,
        3546,
        3561,
        3532       
    ],
    "selectedDepartmentsIDs": [
        10695,
        71,
        72,
        121     
       
    ]
}

现在如何在我的网站中访问此对象 api?以下是我的 api 控制器

[HttpPost("/api/client/{id}/savedata")]
public ActionResult SaveClientDetails(int workflowID, [FromBody] Clientdata data)
{            
    //save operation        
}

下面是我的客户数据class

public class clientdata
{
    public client client{ get; set; }
    public List<selectedUserIDs> selectedUserIDs{ get; set; }
    public List<selectedDepartmentsIDs> selectedDepartmentsIDs { get; set; }
}

和客户class

public class client 
{
    public string clientID { get; set; }
    public string clientData { get; set; }
}

和selectedUserIDs class如下

public class selectedUserIDs
{        
    public int Ids{ get; set; }
}

和selectedDepartmentsIDs class如下

public class selectedDepartmentsIDs
{        
    public int Ids{ get; set; }
}

但是我无法访问网络中的这个复杂对象 api body

邮递员抛出以下错误

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "|10eabbbb-4a15387d9152b7d6.",
    "errors": {
        "$.selectedUserIDs[0]": [
            "The JSON value could not be converted to System.Collections.Generic.List`1[ClientCoreAPI.Client.Entity.selectedUserIDs]. Path: $.selectedUserIDs[0] | LineNumber: 6 | BytePositionInLine: 12."
        ]
    }
}

您对 clientdata 的定义与 json 文件不匹配。您有两个选择:

  1. clientdata改为:

     public class clientdata
     {
         public client client{ get; set; }
         public List<int> selectedUserIDs{ get; set; }
         public List<int> selectedDepartmentsIDs { get; set; }
     }
    
  2. 或将json改为

    {
        "client": {
            "clientID": "3529",
            "clientData": "This a test data"
        },
        "selectedUserIDs": [
            { "Ids": 3549 },
            { "Ids": 3567 },
            { "Ids": 3546 },
            { "Ids": 3561 },
            { "Ids": 3532 }       
        ],
        "selectedDepartmentsIDs": [
            { "Ids": 10695 },
            { "Ids": 71 },
            { "Ids": 72 },
            { "Ids": 121 }
        ]
    }