Home > database >  Posting a list of record in Swagger
Posting a list of record in Swagger

Time:07-23

I have an entity called experience that contains a list of Activities. How do I set my controller and my swagger testing environment that helps me to add and experience and at the same time adding a list of record of activities with a foreign key already included ?

CodePudding user response:

I have an entity called experience that contains a list of Activities. How do I set my controller and my swagger testing environment that helps me to add and experience and at the same time adding a list of record of activities with a foreign key already included ?

To transfer related entities in swagger, you can use the [FromBody] attribute, code like this:

public class Experience
{
    public int ExpId { get; set; }
    public string Title { get; set; }
    public List<Activity> Activities { get; set; }
}

public class Activity
{
    public int Id { get; set; }
    public string Description { get; set; }
}

and the API controller method:

[Route("api/[controller]")]
[ApiController]
public class ToDoController : ControllerBase
{
    // POST api/<TodoController>

    [HttpPost("AddExperience")]
    public IActionResult AddExperience([FromBody]Experience experience)
    {
        return Ok(experience);
    }

After that in the swagger UI, send the parameters using json format.

enter image description here

The result as below:

enter image description here

  • Related