ASP.NET Web API Routing

ASP.NET Web API Routing

In this article, I am going to discuss ASP.NET Web API Routing with examples. Please read our previous article where we discussed Cross-Origin Resource Sharing in Web AP with examples. The Routing in Web API is one of the most important concepts that you need to understand. Once you understand this concept, then you can easily learn the internal architecture of ASP.NET Web API pipeline. As part of this article, we are going to discuss the following pointers in detail.

  1. What is Routing in Web API?
  2. Understanding the Route Table.
  3. How the Web API Framework handle an incoming HTTP Request?
What is Routing in Web API?

The Web API Routing module is responsible for mapping the incoming HTTP requests to a particular controller action method. If you are familiar with ASP.NET MVC application, then you can easily understand the Routing as it is very much similar to MVC routing.

The major difference between these two routing mechanisms is that the Web API uses the HTTP method, not the URI path, to select the action. You can also use MVC style routing in Web API which we will discuss in our upcoming articles.

Understanding the Routing Table in ASP.NET Web API:

In Web API application, a controller is a class which contains action methods which actually handle the incoming HTTP requests. The public methods of the controller class are called action methods or simply actions. When the Web API Framework receives an HTTP request, it routes that HTTP request to an action method of a controller.

To determine which action method to select or invoke for a particular HTTP Request, the WEB API Framework uses a Routing table. When we create a WEB API application, by default, the Visual Studio creates a default route for our application as shown in the below image.

Routing in ASP.NET Web API

The above route is defined in the WebApiConfig.cs file, which is present inside the App_Start folder as shown in the below image.

Routing in Web API

The routing table in Web API contains each and every route template that we define in the WebApiConfig file. The default route template for the Web API application is “api/{controller}/{id}“. In this template, the term “api” is a literal path segment, and the {controller} and {id} are placeholder variables which will be replaced with the actual value.

How the Web API Framework handle an incoming HTTP Request?

When the ASP.NET Web API Framework receives an HTTP request, it tries to match the URI against one of the route templates available in the routing table. If no route template matches with the URI, then Web API Framework returns 404 error to the client who actually makes the request. For example, the following URIs match with the default route template

Web API using Routing

However, the following URI does not match, because it lacks the “api” segment:

/products/1

NOTE:

The reason for using “api” in the route is to avoid collisions between the Web API and MVC routing. So, you can have “/products” go to the MVC controller, and “/api/products” go to the Web API controller. Of course, if you don’t like this convention, you can change the default route table that also we will discuss.

Once a matching route is found in the Route table. The Web API Framework then selects the controller and the action. To find the controller, the Web API Framework adds “Controller” to the value of the {controller} variable. To find the action, the Web API Framework looks at the HTTP method and then looks for an action method whose name begins with that HTTP method name.

For example, with a GET request, the Web API Framework looks for an action that should start with “Get“, such as “GetProduct” or “GetAllProducts”. This convention only applies to GET, POST, PUT, and DELETE methods. You can enable other HTTP methods by using attributes on your controller that we will discuss in our upcoming article.

Other placeholder variables in the route template, such as {id}, are mapped to action method parameters.

Let us see an example for a better understanding. Suppose you define the following Student controller

Routing in Web API

Here are some possible HTTP requests, along with the action that gets invoked for each request.

ASP.NET Web API Routing

Notice that the {id} segment of the URI, if present, is mapped to the id parameter of the action. In our example, the  Student controller defines two GET methods, one with an id parameter and one with no parameters. It also defines one PUT method which takes one parameter of student type from the request body.

Here another point you need to understand is that the POST request will fail as the controller does not have any “Post” method. 

In the next article, I will discuss the Routing variations in WEB API with examples. Here, in this article, I try to explain Routing in Web API step by step with some examples. I hope this Web API Routing article will help you with your need. 

Leave a Reply

Your email address will not be published. Required fields are marked *