Suppose I have the following classes that are used to store data in a database using EF code first approach and that need to be consumed via a web REST API (.NET core 2.0 restful API)
public class Artist
{
public long ArtistId { get; set; }
public string Name { get; set; }
public ICollection<Song> Songs { get; set; }
}
public class Song
{
public long SongId { get; set; }
public long ArtistId { get; set; }
public string Name { get; set; }
public Artist Artist { get; set; }
}
Also, suppose that I have the following RESTFUL API controller
[Produces("application/json")]
[Route("api/Artists")]
public class ArtistsController : Controller
{
private readonly ApiRepository repository;
// GET: api/Artists/1
[HttpGet("{id}")]
public object GetArtist([FromRoute] long id)
{
return repository.GetArtist(id) ?? NotFound();
}
// GET: api/Artists/1/Song/4
[HttpGet("How do I make this configuration?")]
public object GetSong([FromRoute] long artistId, long songId)
{
// Get the artist from the artistId
// Return the song corresponding to that artist
}
}
At this point, I can access all artistis via https://www.myserver/api/Artists/1
. However, I would like to be able to receive a song from an artist Id. Thus, my questions are as follow:
GetSong([FromRoute] long ArtistId, long songId)
in order to have a route similar to https://www.myserver/api/Artists/1/Songs/1
SongsController
? And how would I configure this controller to stick to the routing described above?To make the the Route as GET: api/Artists/1/Song/4
// GET: api/Artists/1/Song/4
[HttpGet("{artistId}/Song/{songId}")]
public object GetSong([FromRoute] long artistId, long songId)
{
// Get the artist from the artistId
// Return the song corresponding to that artist
}