Mou
Mou

Reputation: 16322

Web API: Need to understand a web api attribute routing

got the code from this url How do I use WebAPI/Rest correctly when other params are needed

i am new in web api attribute routing

[Route("customers/{customerId}/orders")]  
public IEnumerable<Order> GetOrdersByCustomer(int customerId) { ... }  

or  

[Route("customers/{customerId}/orders/{orderId}")]  
public Order GetOrderByCustomer(int customerId, int orderId) { ... } 

what is the meaning of 1st and 2nd route ?

how the first and second url will look like please add the same url for both above action ?

thanks

Upvotes: 0

Views: 479

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 157136

The route is the offset from the application base URL. The route can contain parameter which are translated by the binder, to your method parameters.

So the first route, customers/{customerId}/orders would be called as https://someserver/customers/123/orders (route = customers/123/orders), which will result in a call similar to GetOrdersByCustomer(123).

The second route, customers/{customerId}/orders/{orderId} would be called as https://someserver/customers/123/orders/456 (route = customers/123/orders/456), which will result in a call similar to GetOrderByCustomer(123, 456).

Upvotes: 4

Related Questions