ASP.NET Core API - Post to accept JSON array with multiple items

I'm new to C#. I'm working through a tutorial for an ASP.NET Core API, and I'm trying to modify it slightly. The tutorial had the code below in the controller which accepts a single JSON item correctly.
[HttpPost]
public async Task<ActionResult<Mission>> PostMission(Mission mission)
{
if (_context.Missions == null)
{
return Problem("Entity set 'MissionContext.Missions' is null.");
}
_context.Missions.Add(mission);
await _context.SaveChangesAsync();

return CreatedAtAction(nameof(GetMission), new { id = mission.ID }, mission);
}
[HttpPost]
public async Task<ActionResult<Mission>> PostMission(Mission mission)
{
if (_context.Missions == null)
{
return Problem("Entity set 'MissionContext.Missions' is null.");
}
_context.Missions.Add(mission);
await _context.SaveChangesAsync();

return CreatedAtAction(nameof(GetMission), new { id = mission.ID }, mission);
}
I want to modify it to accept a JSON array with multiple items. I've been able to make the modification to accept the array and it works, however the original example returned a result containing the single new record including the new id. I'd like my modifications to do the same but with all the new records created if there are more than one. At the moment it just returns nothing.
[HttpPost]
public async Task<ActionResult<Mission>> PostMission(List<Mission> missions)
{
if (_context.Missions == null)
{
return Problem("Entity set 'MissionContext.Missions' is null.");
}

foreach (Mission _mission in missions)
{
_context.Missions.Add(_mission);
}

await _context.SaveChangesAsync();
//return CreatedAtAction(nameof(GetMission), new { id = mission.ID }, mission);
return NoContent();
}
[HttpPost]
public async Task<ActionResult<Mission>> PostMission(List<Mission> missions)
{
if (_context.Missions == null)
{
return Problem("Entity set 'MissionContext.Missions' is null.");
}

foreach (Mission _mission in missions)
{
_context.Missions.Add(_mission);
}

await _context.SaveChangesAsync();
//return CreatedAtAction(nameof(GetMission), new { id = mission.ID }, mission);
return NoContent();
}
8 Replies
Anton
Anton2y ago
are you asking what's the rest API convention or just how to do it?
dreamdoctor
dreamdoctorOP2y ago
I think asking about REST conventions is likely to get you ten different answers on the same topic, however anything you'd like to contribute would be appreciated.
Anton
Anton2y ago
idk what's the convention, but you can just return the list change the return type to a list too
dreamdoctor
dreamdoctorOP2y ago
Return which list?
Anton
Anton2y ago
with the missions
ronkpunk
ronkpunk2y ago
have you tried this?
return Ok(missions);
return Ok(missions);
maybe your returned missions list hasn't any ID on list items, if so, you have to retrieve data from context (if so, remember to return missions.ToList() )
FusedQyou
FusedQyou2y ago
_context.Missions.AddRange is a thing by the way. No need to iterate the missions seperately
dreamdoctor
dreamdoctorOP2y ago
@Stefano Roncalli @FusedQyou Thanks! Works great now.
Want results from more Discord servers?
Add your server