2

My project: ASP.Net Web Api, Dot.Net Core 2.0, VS 2017.

This is my model:

public class AddressData
{
    public string id { get; set; }
    public string address { get; set; }
    public int number { get; set; }
    public string complement { get; set; }
}

This is my controller

[Route("api/[controller]")]
public class ValuesController : Controller
{
    [HttpPost]
    public IActionResult Post([FromBody] List<AddressData> value)
    {
        if (value == null)
            return BadRequest();

        return Ok();
    }

}

This is my Json:

{
"value" : [
    {
        "id" : "08921619810",
        "address" : "ilicinia",
        "number" : 154,
        "complement": ""
    },
    {
        "id" : "12345678910",
        "address" : "candido figueiredo",
        "number" : 581,
        "complement": "ap 15"
    }
]
}

the request parameter 'value' is always NULL. If I change the controller method signature to

    [HttpPost]
    public IActionResult Post([FromBody] AddressData value)

remove and use the following Json

{
"id" : "08921619810",
"address" : "ilicinia",
"number" : 154,
"complement": ""
}

Everything works fine. If I change to [FromForm], it works, but the List of objects contains no elements (Count property = 0).

I'm using Postman, configured to send POST messages in "application/json" for content type.

Can someone tell me where I'm doing wrong?

1 Answer 1

3

There is a mismatch between the object you're sending in JSON, and the C# POCO object you're trying to deserialise into. The JSON you're sending is not a list of AddressData, it is an object that looks like this is C#:

using System;
using System.Collections.Generic;

namespace Yournamespace
{
    public class AddressDataList
    {
        public List<AddressData> value { get; set; }
    }

    public class AddressData
    {
        public string id { get; set; }
        public string address { get; set; }
        public int number { get; set; }
        public string complement { get; set; }
    }
}

If you change your controller to accept AddressDataList, it should now deserialize correctly:

[Route("api/[controller]")]
public class ValuesController : Controller
{
    [HttpPost]
    public IActionResult Post([FromBody] AddressDataList addressDataList)
    {
        if (addressDataList == null)
            return BadRequest();

        return Ok();
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @tura08. Worked like magic. Just curious: If I'd want to receive a List<> directly within the value parameter, how would I have to format the json? Can you give me a little snippet of that?
A pure list is not valid JSON since there is no key for the list value. So you need to have a object that has a key with the name "value".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.