Working With Attribute Routing In Web API 2

Introduction

In this article we will discuss why we need Attribute Routing and how to work with Attribute Routing in ASP.NET Web API 2.

If you are not aware of Convention Based Routing.. please go through my previous articles on Routing.

Requirements:

  • Visual Studio 2012 with Web API 2 Project Template or Visual Studio 2013 and above.

Definition

We know that the process of matching URI to a particular Action Method is called as Routing. Similarly.. Attribute Routing also does the same thing. But this way of Routing will find the respective action method based on the Attributes placed above the action methods.

Note: Attribute Routing introduced in ASP.NET Web API 2.

Convention-Based Routing to Attribute Based Routing

The routing where we used to have Route Template is usually called Convention Based Routing. Here, the URI will be mapped to an appropriate route template and then respective action method will be invoked. It is good to use and all the routing templates will be located in a unique location.

Though it is good, a major problem is there with the Convention Based Routing. That is we can't have full control over the URIs and we can't create URIs which describes hierarchies of resource. So that new type of Routing is been introduced. That Routing is called as Attribute Routing.

The problem with the Convention Based Routing is resolved by Attribute Routing. We can create more user friendly URIs with the help of this type of Routing. We can create URIs which can easily explains the children of an entity. For example Project has Phases. This text can be described with the URI itself.

http://localhost/project/1526/phases

The above URI shows that we are looking for phases of project which is having ID 1526. This type of flexible URIs can be easily implemented by Attribute Routing. But it can be little hard with Convention Based Routing.

Attribute Routing With an Example:


Step 1: Create an Web API Project using Visual Studio.

Web API Project

Step 2: Create Models called Project and Phases.

Right click on Models and Models, Add, then Class.

Class

And Project model is,

Project mode

Phases Model is,

Phases Model

Step 3 Create a Web API's empty controller called Phases Controller. Now create the following action methods.

Create a Web API

Note: In the above controller we didn't implemented Attribute Routing.

Step 4: In order to work with Attribute Routing, we need to enable this routing in WebAPIConfig.cs by adding the following code.

Attribute Routing

  1. config.MapHttpAttributeRoutes();  
Note: We can use both the Routings in single projects.

Step 5: Add Route to the Action method of the Phases Controller as in the following code snippet:

Add Route

Step 6:

If we run the above code in Rest Client, we will get the response as desired.

get the response

Similarly, we can perform attribute routing with Post, Put, Delete of HTTP verbs.

Note: Finding the exact Route follows the same rules of Convention Based Routing. In the Convention Based Routing all the Route templates located in WebAPIConfig.cs where as in Attribute Routing.. Routes are located above the Action Methods.

Now, we will discuss another interesting topics Route Names and Route Prefix in Attribute Routing.

Route Names

This is also an interesting topic in Attribute Routing. We can give the names to Attribute Routing as well. The Route Names are useful to generate links.

Route Names

Route Prefix


If you observe the Phases Controller, all the Routes are started with "Project/{ProjectId}". In order write this text above of the each action method.. we can write above the Controller as well. This means we are adding "Project/{ProjectId}" text to each Route of the action method in the controller. This way of writing route is called Route Prefixing.

Route Prefix

Note: We overload the Route Prefix by using the "~" to route. You could observe this in Insert Action method.

Conclusion

We have discussed about Attribute Routing in Web API 2 and why and where need to use this type of Routing.

 


Similar Articles