Attribute Routing in ASP.Net Web API


This article provides a simple example of attribute routing in the Web API. Here I will show you how the attribute "Routes" works. Routing is the process of matching  the URI with the specified action. In routing we use the routing table to determine the action to be performed depending on the action being called. Attribute routing uses the attribute for defining the routes.

Now let's see an example of attribute routing in the Web API.

  1. First we create a Web API application as in the following:
    • Start Visual Studio 2012.
    • From the Start window select the "New Project".
    • Then Select "Installed" -> "Visual C#" -> "Web".
    • Select "ASP.NET MVC4 Web Application" and click on the "OK" button.

      Select MVC4 Application

    • From the "MVC4 Project" window select "Web API".

      Select Web API
    • Click on the "OK" button.
  1. Now we install the "Attribute Routing package".
    • From the "Tools" menu.
    • Select "Library Package Manager" -> "Manage NuGet Package for Solutions".
    • In the search box type "AttributrRouting.Webapi".
    • Now select AttributeRouting (ASP.NET Web API) and click on "Install".

      Attribute Routing Package
    • It adds an "AttributeRoutingHttpConfig.cs" file in the "App_start" folder to your project.

      Display Attribbute Routing file
  1. Now in the API controller "Values Controller" make some changes to the code.

    First we add a "[Prefix]" attribute in the controller:
    1. [RoutePrefix("Values")]    
    2. public class ValuesController : ApiController    
    3. {    
    4. }
    Now add some other attribute for getting the record:
    1. [GET("GetAllvalue")]    
    2. public IEnumerable<string> Get()    
    3. {    
    4.     return new string[] { "value1""value2" };    
    5. }    
    6. [GET("GetValue/{id}")]    
    7. public string Get(int id)    
    8. {    
    9.     return "value";    
    10. }
    [GET("GetAllValue")] is for finding all the values of the specified method. And [GET("GetValue/{id}")] is for finding the value on the basis of the id.

    Now execute the application. For execution of the application we need Fiddler. First we execute the application by pressing F5, then copy the URL.

    copy the URL from this page
  • Open the fiddler.
  • Click on the Composer tab and paste the copied URL.
  • And change it as in the following:
    The Output will be as:

    GetAll value in fiddler
    Now for finding the value depending on id set the URL as: http://localhost:41905/Values/GetValue/1. The output will be as:

    Get Value through ID
  1. We will now use the Optional parameter; add the following code in the Controller:
    1. [GET("Name/{name?}")]  
    2. public string GetName(string name)  
    3. {  
    4.     return name;  
    5. }
    Execute the application in Fiddler with the URL "http://localhost:41905/Values/Name/Centro".

    Output with optional parameter
  1. Now set the default string in the route and find the output with the URL "http://localhost:41905/values/VName". The output will be:

    output with default string
  1. Now define the Action precedence. It is used for defining the primary route. Find the output with the URL "http://localhost:41905/values/Index".

    Action precedence output

Now we will see all the code of the "ValuesController"; it is looks like this:


  1. using AttributeRouting;  
  2. using AttributeRouting.Web.Http;  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Linq;  
  6. using System.Net;  
  7. using System.Net.Http;  
  8. using System.Web.Http;  
  9. namespace MvcApplication20.Controllers  
  10. {  
  11.     [RoutePrefix("Values")]  
  12.     public class ValuesController : ApiController  
  13.     {  
  14.          [GET("GetAllvalue")]  
  15.         public IEnumerable<string> Get()  
  16.         {  
  17.             return new string[] { "value1""value2" };  
  18.         }  
  19.         [GET("GetValue/{id}")]  
  20.         public string Get(int id)  
  21.         {  
  22.             return "value";  
  23.         }  
  24.         [GET("Name/{name?}")]  
  25.         public string GetName(string name)  
  26.         {  
  27.             return name;  
  28.         }  
  29.         [GET("VName/{name=Tavera}")]  
  30.         public string GetVName(string name)  
  31.         {  
  32.             return name;  
  33.         }  
  34.         [GET("GetAllValue", ActionPrecedence = 1)]  
  35.         [GET("Index")]  
  36.         public IEnumerable<string> GetVal()  
  37.         {  
  38.             return new string[] { "value1""value2" };  
  39.         }  
  40.     }  
  41. }