This project is read-only.

I just want to point a Route to a Method...

Topics: Web Api
Oct 25, 2011 at 8:28 PM
Edited Oct 25, 2011 at 8:29 PM

This seams to me like the most basic scenario for a web framework of any type: I have a uri pattern. When that pattern is matched, please invoke a method. Like this:

customers/{customerId}/orders/{orderId}   invokes =>  CustomerOrderHandler.Get(string customerId, string orderId)

I can't figure out how to do this. Has anyone accomplished this? I thought the new WebApiConfiguration would allow this, but it only allows uri prefixes, not actual routes.

Oct 25, 2011 at 8:46 PM
Edited Oct 25, 2011 at 8:48 PM

Is this what you are trying to do?

   class Program
        static void Main(string[] args)
            var baseAddress = "http://localhost:8080/OrderService";
            using (HttpServiceHost host = new HttpServiceHost(typeof(OrderService), baseAddress))

    public class Order { ... }

    public class OrderService
        [WebGet(UriTemplate = "customers/{customerId}/orders/{orderId}")]
        Order GetOrder(string customerId, string orderId)

The uri such as http://localhost:8080/OrderService/customers/maggieying/orders/b38fkc will invoke the GetOrder(...) service operation.  Please let us know if you have further question.

Maggie Ying

Oct 25, 2011 at 8:54 PM

No. Our architecture dictates that the routes must come from somewhere else. They cannot be defined in the attribute.

Oct 25, 2011 at 8:57 PM

I want to be able to do the same thing that I can with the MVC framework:

                new { controller = "Customers", action = "GetOrdersForCustomer" }

Oct 25, 2011 at 9:19 PM
EisenbergEffect wrote:

No. Our architecture dictates that the routes must come from somewhere else. They cannot be defined in the attribute.

Yeah, the attribute requirement is a bummer in terms of portability.

Oct 25, 2011 at 9:32 PM

Well, I had this working with Preview 4...with some code I got from somewhere...which shall remain nameless, but it's broken with the new build and I don't know how to fix it ;(

Oct 25, 2011 at 9:54 PM

What would be a nice model to do the 

    customers/{customerId}/orders/{orderId}   invokes =>  CustomerOrderHandler.GetOrder(string customerId, string orderId)


Something like this?

config.Map<CustomerOrderHandler>("customers/{customerId}/orders/{orderId}", s => s.GetOrder(default(string),default(string))


Oct 25, 2011 at 10:05 PM

I'll settle for any api at all :)

Oct 25, 2011 at 10:14 PM

Currently Web API doesn’t support parameterized routes through ASP.NET Routing. You have to use UriTemplates instead. Adding full support for ASP.NET routing is definitely something that we are working on though, so stay tuned!

I’m curious, how did you get this working on Preview 4?

Daniel Roth

Oct 25, 2011 at 10:17 PM

It requires some "low level" coding...I didn't write the code myself. I'm not sure I can wait for the official support. Either I've got to dive deep into WCF or I've got to refactor our entire service layer. I think I'd rather refactor everything than touch WCF.

Oct 25, 2011 at 10:24 PM

How does your existing code break on Preview 5?

Daniel Roth

Oct 25, 2011 at 10:26 PM

Also, could you please provide me with more details about your architecture constraints? Why exactly can’t you use attributes to specify UriTemplates?

Daniel Roth

Oct 26, 2011 at 10:56 AM


Does this fits your requirements (see the Run method)?

Some observations:

* I'm changing the operation description on the OnConfigureEndpoint method, however I'm not sure if this completely "legal". However, "it works on my machine".

* The URI template configuration requires the knowledge of the operation parameters, which seems strange since this is done "far away" from the operation.

* It only works for GET. However, adding support for other HTTP methods is trivial.



Oct 26, 2011 at 9:12 PM

I've got everything working now. Not sure if I can share or not. I'll find out.