0

guys. I just want to ask a question about json transfer in asp .net core controller.

I know if I want to read json like

{
FirstName: "Zhen",
LastName: "Wang"
}

from request.body, I need to bind this to an object like:

public class Person  
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class PersonController : ApiController  
{
    [HttpPost]
    public Person Index([FromBody]Person person)
    {
        return person;
    }
}

But, if there is not only a Person in json, but also a Building like:

    {
      Person: {
        FirstName: "Zhen",
        LastName: "Wang"
      },
      Building:{
        Address:"Willis Street"
      }
    }

Do I have to build a PersonAndBuilding to recive person and building data from request.body?

public class PersonAndBuilding  
{
    public Person Person { get; set; }
    public Building Building { get; set; }
}

public class PersonController : ApiController  
{
    [HttpPost]
    public Person Index([FromBody]PersonAndBuilding personAndBuilding )
    {
        return personAndBuilding.Person;
    }
}

I think this is weird because this means I have to build a new Class for each POST controller in my program.

Could anybody please give me some idea?

2
  • Not entirely sure, but you should just be able to create a parent class with a single Person property; parent class name shouldn't matter; and exclude the Building class / property all together. Sep 19, 2017 at 0:27
  • how are you sending the json? by what means? Sep 19, 2017 at 1:11

2 Answers 2

1

Data you POST is transferred in one of a few ways, though either the Body/Form or the URL (or both!) is most common. The content of the Request body can usually only be read once, which is why you can only have one FromBody attribute per parameter.

It is common to use DTOs (Data Transfer Objects) when sending or receiving data to/from your WebAPI. DTOs are responsible for handling precisely as much data as is necessary without unnecessary database junk, or fields you don't need/want exposed.

In this scenario, yes, you will need a class which specifically contains one Building and one Person. For simplicity you can define your DTO in the same class / file, or for a more elegant and organized solution define your DTOs in ./DTOs/ or ./Models/DTOs/.

A slight alternative is to modify your REST structure so your Person object is identified by an Id, such that your Route looks like /api/Person/{personId}. Then your method signature can look something like...

[HttpPost("{personId}")]
public IActionResult Index([FromBody] Building building, [FromRoute] int personId) {
    var person = ctx.Persons.Single(p => p.Id == personId);
    //you have a building and person.
}
0

No, you can just do this:

public class PersonController : ApiController  
{
    [HttpPost]
    public Person Index(Person Person,  Building Building)
    {
        return Person;
    }
}

It depends on how you serialize the json before you pass it too.

3

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.