Web API Attribute Routing

In my continue research into Web API I came across a new feature in Web API 2 called Attribute Routing.  This feature makes it possible to define routes at the API level.  It makes it easier to define complex RESTful URIs where there are child resources like “/customer/123/orders”.  It also gives you the ability to have multiple GETS, POSTS and PUTS on the same controller.

The traditional style of routing is called Convention-Based Routing and you can mix both in the same application.

On a side note, there is also the ActionName attribute.  It doesn’t give you the ability to define the route but it does make it possible for you to have multiple GETS, POSTS and PUTS on the same controller.

You do have to enable attribute routing.  This is done in the WebApiConfig.Register method: config.MapHttpAttributeRoutes();

This ASP.NET link describes attribute routing in more detail: http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s