Although informative, there appears to be no concrete guidance from Microsoft or otherwise in terms of how best to implement links for your resources (i.e. object model implemented using DTOs at the service layer). If we are moving towards using WebAPI to
implement a true RESTful service, hyperlinking is crucial and I would have expected built in support / guidance for this in WebAPI.
A simple example I'm trying to implement this for is using a Contact entity which has a collection of Addresses and a collection of SupportIncident. The class definitions based on the first article I've referenced would look something like so:
public class Link
{
public Uri Uri { get; set; }
public string Rel { get; set; }
public string Name { get; set; }
}
public class ContactDTO
{
public int ID { get; set; }
[Required]
public string Name { get; set; }
public IList<AddressDTO> Addresses { get; set; }
public IList<IncidentDTO> Incidents{ get; set; }
public IList<Link> Links { get; set; }
}
WebAPI is great at exposing this model over HTTP (xml/json) and the GET/PUT/POST/DELETE action on the resource but in order to build a true RESTful service I would like to know if a) Is there inherent support for link generation in WEBAPI? b) Is there proper
guidance for exposing the model above containing links to resources and how should the API handle PUT/POST if a client sends links as part of the payload. Should these be ignored?
There is no one, right way to expose links. Some media types have support for links, others don't. You can pass links through HTTP headers if you like. Web API stays true to RFC 2616, so it doesn't force an opinion on how you expose links. Glenn's post is
a good guideline. (He works for Microsoft.) Also, you may want to take a look at
Howard Dierking's RestBugs, which does some similar link exposure.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
It works with a csutome mediatypeformater and hook into the serialzing of JsonConvert (Newtonsoft) with a custom JsonCOnverter Class. But I dont like it-
patelabhijee...
Member
4 Points
7 Posts
Hyperlinking in WebAPI
Feb 09, 2013 09:47 PM|LINK
Is there any solid guidance for generating hyperlinks for your resources in ASP.NET WebAPI? I've read a couple of posts on this:
http://codebetter.com/glennblock/2012/01/08/hypermedia-and-web-api-design-brain-dump-and-samples/
http://blog.ploeh.dk/2012/04/17/HyperlinkingWithTheASPNETWebAPI.aspx
Although informative, there appears to be no concrete guidance from Microsoft or otherwise in terms of how best to implement links for your resources (i.e. object model implemented using DTOs at the service layer). If we are moving towards using WebAPI to implement a true RESTful service, hyperlinking is crucial and I would have expected built in support / guidance for this in WebAPI.
A simple example I'm trying to implement this for is using a Contact entity which has a collection of Addresses and a collection of SupportIncident. The class definitions based on the first article I've referenced would look something like so:
public class Link { public Uri Uri { get; set; } public string Rel { get; set; } public string Name { get; set; } } public class ContactDTO { public int ID { get; set; } [Required] public string Name { get; set; } public IList<AddressDTO> Addresses { get; set; } public IList<IncidentDTO> Incidents{ get; set; } public IList<Link> Links { get; set; } }WebAPI is great at exposing this model over HTTP (xml/json) and the GET/PUT/POST/DELETE action on the resource but in order to build a true RESTful service I would like to know if a) Is there inherent support for link generation in WEBAPI? b) Is there proper guidance for exposing the model above containing links to resources and how should the API handle PUT/POST if a client sends links as part of the payload. Should these be ignored?
panesofglass
Member
730 Points
237 Posts
Re: Hyperlinking in WebAPI
Feb 12, 2013 11:12 AM|LINK
There is no one, right way to expose links. Some media types have support for links, others don't. You can pass links through HTTP headers if you like. Web API stays true to RFC 2616, so it doesn't force an opinion on how you expose links. Glenn's post is a good guideline. (He works for Microsoft.) Also, you may want to take a look at Howard Dierking's RestBugs, which does some similar link exposure.
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Hyperlinking in WebAPI
Feb 13, 2013 04:09 PM|LINK
Some good guidence is also available in a this msdn magzine blog
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
preishuber
Participant
1019 Points
226 Posts
Re: Hyperlinking in WebAPI
Feb 14, 2013 04:55 PM|LINK
I am actually sitting on the same issue
It works with a csutome mediatypeformater and hook into the serialzing of JsonConvert (Newtonsoft) with a custom JsonCOnverter Class. But I dont like it-
http://www.ppedv.de