5

I have seen several tutorials in which to send a json object with POST method.

[HttpPost]
public async Task<IActionResult> createEntity([FromBody]Entity entity)
{
    try
    {
         await _repository.Entity.CreateEntityAsync(entity);
         return Ok();
     }
     catch (Exception ex)
     {
          return StatusCode(500, "Internal server error");
      }
}

This does the job correctly

Now if I want to send:

{ "ID":1, "data":[ { "value1":"xxxx", "value2":"yyyy" }, { "value1":"zzzz", "value2":"wwww" } ] }

if you could recommend me what would be the best option to work this

0

1 Answer 1

9

Make a DTO class as follows:

public class YourDto
{
   public int ID {get; set;}
   public List<Data> Data {get; set;}
}

Where Data is as follows:

public class Data
{
   public string Value1 {get; set;}
   public string Value2 {get; set;}
}

Then in your POST method:

[HttpPost]
public async Task<IActionResult> createEntity([FromBody]YourDto yourDto)
{
    try
    {
         // do whatever you want to do with the yourDto object

         return Ok();
    }
    catch (Exception ex)
    {
          return StatusCode(500, "Internal server error");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.