Routing in ASP.NET Web API

Introduction

In this article, we will define how the ASP.Net Web API transfers the HTTP request to the controller.

Routing Table


We use controllers in the ASP. Net Web API. These controllers work as a class for handling the HTTP request. After receiving the request by the API framework it transfers the request to perform an action. The controller class has public methods; these methods are called action methods.

We use the routing table to determine the action to be performed depending on that action being called. By default the Visual Studio creates a default route for the web API.

  1. public static void Register(HttpConfiguration config)  
  2. {  
  3.     config.Routes.MapHttpRoute(  
  4.         name: "DefaultApi",  
  5.         routeTemplate: "api/{controller}/{Id}",  
  6.         defaults: new { Id = RouteParameter.Optional }  
  7.     );  
  8. }  

We defined this route in the Web.Config.cs file:
 
 config.jpg

We know that the web API is a framework that receives the HTTP request for performing the action. After receiving the request it matches the URI of the route templates to the routing table. If there is not a match then it returns the error to the client. For example, there are various URI matches to the default route:

  • /api/products/
  • api/products/1
  • api/Items/
If there is no API segment then it does not match. For example:
  • /products/1

If the route matching is done then the Web API selects the controller and action.

  • To find the controller the Web API adds the controller for the value of its variable.
  • To find the action the Web API first sees the HTTP method and the action. It matches the method's begin name with the HTTP method name. For example if there is a Get request, then the Web API sees the action and verifies that the action starts with Get.... such as GetAllItems, GetProducts. And then applies the Get method.
  • And the other variables in the route template work as an action parameter.
Example of the controller: 
  1. public class Items : ApiController  
  2. {  
  3.     public void GetAllItems() { }  
  4.     public IEnumerable<Item> GetItemsById(int Id) { }  
  5.     public HttpResponseMessage DeleteItem(int Id){ }  
  6. }

Here are some HTTP requests with actions:

HTTP Method URI Path Action Parameter
GET api/items GetAllItems none
GET api/items/3 GetItemsById 3
DELETE api/items/3 DeleteItem 3
POST api/items no match  

In this table we note that there is an id segment that represents the id parameter for the action. There are two GET methods, one with a parameter id and another without a parameter.

Routing Variations

Now we will define some variation of the routing table.

HTTP Method

We can specify the method for the action by using the HttpGet, HttpPost, HttpDelete and HttpPut attributes.
 
Here is an example in which we use the FindItem method to specify the GET request:
  1. public class Items : ApiController  
  2. {  
  3.     [HttpGet]  
  4.     public Item FindItem(Id) {}  
  5. }  

If you want to allow more than one HTTP method for an action then we use the AcceptVerb attribute, as in the following:

  1. public class Items : ApiController  
  2. {  
  3.     [AcceptVerbs("GET""HEAD")]  
  4.     public Item FindItem(Id) { }  
  5.     [AcceptVerbs("MKCOL")]  
  6.     public void MakeCollection() { }  
  7. }

Routing by Action Name

To determine which action is to be performed, the web API uses a different HTTP method. If you want to create the route, you can create it with the action name included in the URI. 

  1. Routes.MapHttpRoute(  
  2.                 name: "DefaultApi",  
  3.                 routeTemplate: "api/{controller}/{Id}",  
  4.                 defaults: new { Id = RouteParameter.Optional }  
  5. );

The parameter in the route table has the same name as the action name in the controller. Let's see an example as in the following method:

  1. public class Items : ApiController  
  2. {  
  3.     [HttpGet]  
  4.     public string Details(int Id);  
  5. }  

For overriding the action name we use the ActionName attribute, as in the following:

  1. public class Items : ApiController  
  2. {  
  3.     [HttpGet]  
  4.     [ActionName("Tulips")]  
  5.     public HttpResponseMessage GetTulipsImage(int Id);  
  6.     [HttpPost]  
  7.     [ActionName("Tulips")]  
  8.     public void AddTulipsImage(int id);  
  9. }  

Non-Action

A non-action is an action that you want to prevent, in other words you do not want to call it. For that we use the NonAction attribute.

  1. public class Items : ApiController  
  2. {  
  3.     [NonAction]  
  4.     public string GetPrivateData() { }  
  5. } 


Similar Articles