According to MSDN, Routing is how Web API matches a URI to an action. Web API 2 supports a new type of routing, called attribute routing.
Introduction
This article demonstrates how to enable attribute routing and describes the various options for attribute routing.
Prerequisites
Visual Studio 2013 or Visual Studio Express 2013
Use NuGet Package Manager to install the necessary packages. From the Tools menu in Visual Studio, select Library Package Manager, then select Package Manager Console. Enter the following command in the Package Manager Console window:
Install-Package Microsoft.AspNet.WebApi.WebHost
What is Attribute Routing?
As the name implies, attribute routing uses attributes to define routes. Attribute routing gives us more control over the URIs in our web API. We can easily create URIs that describe hierarchies of resources.
We can simply add an attribute to the controller action as shown below
[Route("students/{studentId}/orders")]
public IEnumerable GetOrdersByStudent(int studentId) { ... }
How to enable Attribute Routing?
For that, we have to select the WebApiConfig.cs inside the App_Start Folder.After that call MapMvcAttributeRoutes is as shown below.This extension method is defined in the System.Web.Http.HttpConfigurationExtensions class.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
}
}
We can also combine attribute routing with convention-based routing. To define convention-based routes, call the MapHttpRoute method.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
How to add Route Attributes?
Here is an code snippet of a route defined using an attribute
public class OrdersController : ApiController
{
[Route("students/{studentId}/orders")]
public IEnumerable FindOrdersByStudent(int studentId) { ... }
}
The [Route] attribute defines an HTTP GET method. The string "students/{studentId}/orders" is the URI template for the route.
Note: Here the "{studentId}" parameter in the route template matches the name of the studentId parameter in the method.It can also have more parameters like
public class OrdersController : ApiController
{
[Route("students/{studentId}/orders")]
public IEnumerable FindOrdersByStudent(int studentId,int orderId) { ... }
}
How to use Route Prefixes?
Routes in a controller will start with the same prefix as shown below
public class ProductsController : ApiController
{
[Route("api/products")]
public IEnumerable GetProducts() { ... }
[Route("api/products/{id:int}")]
public Book GetProduct(int id) { ... }
[Route("api/products")]
public HttpResponseMessage CreateBook(Product product) { ... }
}
How to set common Route Prefixes?
We can specify a common prefix for an entire controller to that we can use [RoutePrefix] attribute
It's like this : [RoutePrefix("api/products")]
[RoutePrefix("api/products")]
public class BooksController : ApiController
{
// GET api/books
[Route("")]
public IEnumerable Get() { ... }
// GET api/products/5
[Route("{id:int}")]
public PRoduct Get(int id) { ... }
// POST api/products
[Route("")]
public HttpResponseMessage Post(Product product) { ... }
}
Conclusion
In this article we have seen how to enable Attribute Routing..In coming articles we will see more on Attribute Routing
Reference
http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2