我有一个网络服务,我需要接收多个参数,其中一个是列表。
这是网络服务方法:
[HttpPost("complete/{requestId}")]
public IActionResult CompleteRequest([FromBody] RequestComplete requestComplete, int requestId)
{
if (requestId == 0)
{
return BadRequest();
}
if (requestComplete == null)
{
return BadRequest();
}
// execute code
}
类如下所示:
public class RequestComplete
{
public int RequestId { get; set; }
public int UserId { get; set; }
public string emailCC { get; set; }
public string emailSubject { get; set; }
public string calcsNeeded { get; set; }
public string ssiComment { get; set; }
public List<CompleteFileUpload> completeFiles { get; set; }
}
CompleteFileUpload类是这样的:
public class CompleteFileUpload
{
public int RequestFileId { get; set; }
}
从PostMan中,我使用这样的帖子正文:
{
"calcsNeeded" : "4",
"completeFiles": {"RequestFileId": "384"},
"emailCC" : "test@email.com",
"emailSubject": "subject here",
"ssiComment":"Thanks for your request."
}
我的期望是,Postman 发布的 json 将被正确格式化,以便被 CompleteRequest 方法接受。但是,事实并非如此。 如果我删除 json 的完整文件部分,它运行良好。
我错过了什么?
JSON
中 "completeFiles" 的定义没有定义 JSON 数组,它定义了单个对象。 如果定义为:
public class RequestComplete
{
...
public CompleteFileUpload completeFiles { get; set; }
}
尝试将 JSON 更改为:
{
"calcsNeeded" : "4",
"completeFiles": [ {"RequestFileId": "384"}, {"RequestFileId": "123"} ],
"emailCC" : "test@email.com",
"emailSubject": "subject here",
"ssiComment":"Thanks for your request."
}
希望这有帮助