Close

API App Routing

This webpost is more of a ‘back to basics’, but sometimes it’s good to consciously consider behavior that has turned into a routine. In this case I want to talk about routing. To route web service requests (read: web address entered in the browser) to the correct controller and method, we use a so-called routing table. You can find the routing table in file App_Start\WebApiConfig.cs. Check the contents of this file. The WebApi framework tries to match the http request to one of the route templates.

Web API routing is a three-step process. First Web API routing tries to route incoming requests to a specific controller. Next, Web API Routing looks at the Http Action. By default, if you perform a HttpGet the request is routed to a method name that starts with Get. Similarily, if you perform a HttpPost the request is routed to a method name that starts with Post. Next, WebAPI routing checks if a parameter is defined. The default routing parameter includes an optional id parameter. If you perform a Http Get with the id parameter in the URI, Web API routing will route you to a Get operation with parameter id. Note: name of routing parameter must match name of operation parameter. Now let’s say we add a second method that gets products by name. If we simply add a routing parameter with routing parameter name, the routing table will not work. In that case you will have to add an {action} clause to the routing template. The HttpGet URI contains the exact method name for action. Example:

ProductsController
public HttpResponseMessage GetProductById(int id)
public HttpResponseMessage GetProductByName(string name)

config.Routes.MapHttpRoute(
name: “RouteById”,
routeTemplate: “api/{controller}/{action}/{id}”,
defaults: new { id = RouteParameter.Optional }
);

config.Routes.MapHttpRoute(
name: “RouteByName”,
routeTemplate: “api/{controller}/{action}/{name}”,
defaults: new { name = RouteParameter.Optional }
);

Http Get calls:
http://tulipexpressproducts.azurewebsites.net/api/Products/GetProductById/5
http://tulipexpressproducts.azurewebsites.net/api/Products/GetProductByName?name=Yo-yo

If you use a method name that doesn’t start with Get, Post, Put or Delete, you will have to specify an action name via the ActionName annotation. This is not recommended, because you break the uniform contract in that case.

[HttpGet] [ActionName(“RetrieveProductById”)] public HttpResponseMessage RetrieveProductById(int id)

config.Routes.MapHttpRoute(
name: “RouteByAction”,
routeTemplate: “api/{controller}/{action}/{id}”,
defaults: new { id = RouteParameter.Optional }
);

If no match is found a Http 404 – Not Found is returned.

Routing templates are contained in file WebApiConfig.cs in folder App_Start, not as method annotations in controllers. Normally the default template suffices. To conform to the uniform contract {actionname} should never be used in routing templates.

Leave a Reply

Your email address will not be published. Required fields are marked *