// GET: api/Artists?withlinks // Notice the new return result, which includes link relations // Also, try this by changing the "ArtistsLinked" data type to // the "ArtistsLinkedWithTemplateInfo" data type - this action // will return a 'new item template' that is useful to the user public ArtistsLinked Get(string withlinks) { // Get all var fetchedObjects = m.Artists.GetAll(); // Create an object to be returned ArtistsLinked artists = new ArtistsLinked(); // Set its collection property artists.Collection = Mapper.Map<IEnumerable<ArtistWithLink>>(fetchedObjects); // Set the URI request path string self = Request.RequestUri.AbsolutePath; // Add a link relation for 'self' artists.Links.Add(new Link() { Rel = "self", Href = self }); // Add a link relation for each item in the collection foreach (var item in artists.Collection) { item.Link = new Link() { Rel = "item", Href = string.Format("{0}/{1}", self, item.Id) }; } // Return the results return artists; }
// GET: api/Artists?withlinks // Notice the new return result, which includes link relations // Also, try this by changing the "ArtistsLinked" data type to // the "ArtistsLinkedWithTemplateInfo" data type - this action // will return a 'new item template' that is useful to the user public ArtistsLinked Get(string withlinks) { // Get all var fetchedObjects = m.Artists.GetAll(); // Create an object to be returned ArtistsLinked artists = new ArtistsLinked(); // Set its collection property artists.Collection = Mapper.Map <IEnumerable <ArtistWithLink> >(fetchedObjects); // Set the URI request path string self = Request.RequestUri.AbsolutePath; // Add a link relation for 'self' artists.Links.Add(new Link() { Rel = "self", Href = self }); // Add a link relation for each item in the collection foreach (var item in artists.Collection) { item.Link = new Link() { Rel = "item", Href = string.Format("{0}/{1}", self, item.Id) }; } // Return the results return(artists); }