简体   繁体   English

无法将 json object 发布到 asp.net webapi

[英]Not able to post json object to asp.net webapi

I need to pass following json object to my asp.net core 3.1 web api I need to pass following json object to my 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     
       
    ]
}

Now how can i access this object in my web api?现在我如何在我的 web api 中访问这个 object? Following is my api controller以下是我的 api controller

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

and following is my clientdata class以下是我的客户数据 class

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

and clientclass和客户端类

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

and selectedUserIDs class as follows和 selectedUserIDs class 如下

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

and selectedDepartmentsIDs class as follows和 selectedDepartmentsIDs class 如下

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

But I am not able to access this complex object in web api body但我无法在 web api 正文中访问这个复杂的 object

In postman is throwing following error在 postman 中抛出以下错误

{
    "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."
        ]
    }
}

Your definition of clientdata does not match the json file.您对 clientdata 的定义与clientdata文件不匹配。 You have two options:你有两个选择:

  1. Change clientdata to:clientdata为:

     public class clientdata { public client client{ get; set; } public List<int> selectedUserIDs{ get; set; } public List<int> selectedDepartmentsIDs { get; set; } }
  2. Or change the json to或将 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 } ] }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM