How to create outgoing hypermedia links?

Topics: Web Api
Mar 22, 2011 at 6:10 AM

Could use a pointer on generating outgoing links from the rest services.  Ideally there would be a call similar to the MVC urlhelper class that given the route and the parameter list returned the fully qualified URL for that resource. This is as close as I have gotten so far:

                RouteValueDictionary parameters = new RouteValueDictionary { { "action", "Get" }, { "iD", "1" } };
                VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, "Queue", parameters);













Assuming I setup a named route in the global.aspx called Queue pointing to a class called QueueService, I would expect the call to return "\Queue?iD=1" because it find the template that matches the parameters and the webmethod (by matching action). Clearly that is not quite how the system works however.

My goal is to prevent hard coding the urls throughout the applciation.  If I were to go and change the service from "Queue" to "Superqueue" in the Routetable and leave it pointing to the same service class, I don't want to have to manually find and update all of those references.


Mar 22, 2011 at 8:34 PM

Hi There,

My friend Jose Romaniello and I were working on some prototypes for typed links with expressions. We came up with a clean model that you might want to take a look,





Mar 22, 2011 at 9:13 PM

Thanks! Spot on.  I'll note on the blog how well it pans out.

I know a lot of people have not gotten to the point of sending back links using the Rest based APIs, but that become a critical part of the API.  We will need some helper such as the ResourceLinker in the base wcf http libraries.


Mar 22, 2011 at 10:48 PM


Interesting approach from Jose. We are looking at link generation, and I imagine long term we will have some way to provide a resource map (not necessarily dependent on routes) that will allow you to generate similar links. I really like the lambda approach as it removes any magic strings.

Mar 22, 2011 at 10:55 PM

One caveat to this approach is that it is assuming there is a one to one correspondence between the variables definied in the uri template and the parameters of the method. However that is not necessarily the case. A custom processor could sit in the middle and parse information coming from the uri including template variables and transform those into other values. In those cases you won't be able to really work back from the values passed in the expression to the appropriate uri template.


Mar 23, 2011 at 9:48 PM

Glen, thank you for taking your time to read the post.

You are right about it, the only thing I'm doing is to ignore those parameters that aren't part of the uri template which is prety vague.

My first design was something like;

GetUri<Resource>(r =>r.Method(null, null, null), par1, par2);

then i was runing the UriTemplate by positions..

But still not happy with that

Dec 20, 2011 at 4:10 PM

Where are we at with link generation in WCF Web API, Preview 6+?

I've spent the last two hours looking around in vain to see how to do link generation for embedding in outgoing XML representations from my MVC 3-hosted WCF Web API-based REST service.  I want to generate absolute URIs.

Is it supported yet?  Did I miss it?  How to do it?

If not yet supported, do you have an idea when it will be?


Dec 22, 2011 at 12:02 PM
Edited Dec 22, 2011 at 12:06 PM

Im desperately needing this hypermedia links support as well. I have been looking for it for many hours, but surprisingly cant seem to find much on the mighty internet. I would be very surprised if this is not a supported feature, because you can barely build a rest api without hypermedia support. Without it, how would you represent navigability for clients to transition between states/resources in our rest api? (which is pretty much all the letters in the REST acronym)

Dec 22, 2011 at 12:40 PM

@hendryluk, I've concluded that this feature isn't supported yet.  At least, I can't find it.  As you say, link generation is pretty fundamental for REST (although Wcf Web API isn't necessarily a REST framework).  I am building a "web api", don't want to call it a REST service, but I need link generation too.

For now (and maybe forever) I've added a static Init method to my one and only service type, which is called by the hosting environment (MVC).  The Init method takes args for: URI scheme, URI authority and the service's route prefix.  With this info the Init method builds up a static list of information about all of the service type's resources, including their absolute URIs.  The latter are used in outgoing links embedded in outgoing resource representations.

Okay, I'm using magic strings, but I've got a written spec for all my resources which includes what URIs identify them.  And hey: "cool URIs don't change", right?

Maybe even when the WCF Web API supports dynamic link generation, I will stick with this scheme.

Dec 22, 2011 at 1:10 PM

Web API is going to be integrated with the ASP.NET routing framework that MVC uses, and that supports generating links from routes.  However as Glenn states things aren't always that simple when you have handlers in between that can be changing the URLs (so whats externally visible isn't the same as the uri templates on the methods).

I'm really looking forward to the next release and hope to see something about this there, but for now I've rolled my own like Andrew did.

Dec 22, 2011 at 9:25 PM
Edited Dec 22, 2011 at 9:30 PM

Thanks @andrewwebb, I'll probably be doing a similar thing as well.

I'm not really much concerned about being strong-type. I don't have any particular dislike with something loose-type (like link-generations in ASP.NET MVC).

What I would like to see (in future WCF webapi) is a way to represent links/transitions in our data models to be rendered as URIs by the formatters.