Skip to content

Instantly share code, notes, and snippets.

@killnine
Last active September 28, 2019 19:57
Show Gist options
  • Save killnine/4e3f8c29074f61036718a2214f6608e3 to your computer and use it in GitHub Desktop.
Save killnine/4e3f8c29074f61036718a2214f6608e3 to your computer and use it in GitHub Desktop.
Handling REST collections of child entities...
/*
When a user posts multiple songs to the controller, I want it to redirect back to
the Get endpoint. However, I don't believe asp.net's Routing handles that.
*/
[Route("api/songbooks/{songbookId}/songcollections")]
public class SongCollectionController : Controller
{
[HttpGet("{id}", Name = "GetSongCollection")]
public IActionResult GetSongCollection(Guid songbookId, [ModelBinder(BinderType = typeof(ArrayModelBinder))]IEnumerable<Guid> ids)
{
//..SNIP..
}
[HttpPost]
public IActionResult Post(Guid songbookId, [FromBody]IEnumerable<SongCreate> songCollection)
{
//..SNIP..
_repository.Save(songbook);
var idsAsString = string.Join(",", songs.Select(sb => sb.Id));
return CreatedAtRoute("GetSongCollection", new { SongbookId = songbook.Id, Ids = idsAsString }, _mapper.Map<IEnumerable<SongResult>>(songs),);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment