我似乎已经遇到了Web API的一个有趣的问题-使用此控制器操作,在左花括号处有一个断点来检查thing

[HttpPut]
public void Test([FromBody]object thing)
{
}


如果我提交以下请求(作为CURL),则会得到期望值,该期望值是包含主体数据的JObject:

curl -X PUT -H "Content-Type: application/json" -d '{
    "Name": "Joe",
    "Key": { "Key": "Value" }
}' "http://localhost/api/TestController/Test"


也就是说,其中Name是Joe且Key是另一个包含Key = Value的匿名对象。

但是,如果我稍稍更改此设置以提交Keys数组,则会得到null。没有错误,只是null

curl -X PUT -H "Content-Type: application/json" -d '{
    "Name": "Joe",
    "Keys": [{ "Key": "Value" }]
}' "http://localhost/api/TestController/Test"


为什么会这样呢? HTTP动词似乎无关紧要(我已经尝试过POST和PUT),并且使用dynamic代替object是同一回事。

这是一些屏幕截图:
c# - 发送对象数组时,Web API [FromBody]参数为null-LMLPHP
c# - 发送对象数组时,Web API [FromBody]参数为null-LMLPHP

最佳答案

在Postman中进行测试时,您的代码工作正常。

c# - 发送对象数组时,Web API [FromBody]参数为null-LMLPHP

c# - 发送对象数组时,Web API [FromBody]参数为null-LMLPHP

关于c# - 发送对象数组时,Web API [FromBody]参数为null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38312672/

10-11 22:09
查看更多