简体   繁体   English

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

[英]Web API [FromBody] parameter is null when sending an array of objects

I seem to have run into an interesting problem with Web API - using this controller action, with a breakpoint on the opening brace to inspect thing : 我似乎已经遇到了Web API的一个有趣的问题-使用此控制器操作,在左花括号处有一个断点来检查thing

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

If I submit the following request (as CURL), I get the expected value which is a JObject containing the data from the body: 如果我提交以下请求(作为CURL),则会得到期望值,该期望值是包含主体数据的JObject:

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

That is, an anonymous object where Name is Joe and Key is another object containing Key = Value . 也就是说,其中Name是Joe且Key是另一个包含Key = Value对象的匿名对象。

However, if I change this ever-so-slightly to instead submit an array of Keys , I get null . 但是,如果我稍稍更改此设置以改为提交Keys数组,则会得到null No error, just null . 没有错误,只是null

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

Why is this happening? 为什么会这样呢? The HTTP verb doesn't seem to matter (I've tried POST and PUT), and using dynamic instead of object is the same thing. HTTP动词似乎无关紧要(我尝试过POST和PUT),并且使用dynamic代替object是同一回事。

Here's some screenshots: 这是一些屏幕截图: 在此处输入图片说明 在此处输入图片说明

Your code is working fine when testing in Postman. 在Postman中进行测试时,您的代码工作正常。

在此处输入图片说明

在此处输入图片说明

Turns out I had the global JSON setting for MaxDepth set to 2 elsewhere in my code, which was running before the controller action. 原来我在代码中的其他位置将MaxDepth的全局JSON设置设置为2 ,该设置在控制器动作之前运行。 Why this returns null and not an empty object is still beyond me, but changing this to 3 corrects the issue and functions as expected. 为什么它返回null而不是空对象的原因仍然不在我的范围内,但是将其更改为3可以解决问题并按预期运行。

Sorry for the confusion everyone, thanks for the answers! 抱歉给大家带来的困扰,谢谢您的回答!

i think it all depends on how you wish to work with it, if you want to get a strongly typed model you can always create one like user3658685 said. 我认为这完全取决于您希望如何使用它,如果您想获得一个强类型化的模型,您可以随时创建一个像user3658685所说的模型。

another thing you can do is bind the data to JObject and then serialize using Newton-soft Serializer: 您可以做的另一件事是将数据绑定到JObject,然后使用Newton-soft Serializer进行序列化:

public void Post([FromBody] JObject value)
{ 
    //instead of object you can use any type you wish to cast
    object obj = value.ToObject<object>();
}

WebApi uses model mapping class to try to map json object with the argument og your WebApi method. WebApi使用模型映射类来尝试使用WebApi方法的参数映射json对象。 You can resolve this by creating a class with all attributes matching your json object. 您可以通过创建一个具有与json对象匹配的所有属性的类来解决此问题。

public class Contract {
   public string Name { get; set; }
   public dynamic[] Keys { get; set; }
}

And changing the signature of your WebApi method with 并更改您的WebApi方法的签名

public void Post([FromBody] Contract value)
{
}

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

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