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 API 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 the ASP.NET Web API pipeline.
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 the 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 that contains action methods that 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.
The above route is defined in the WebApiConfig.cs file, which is present inside the App_Start folder.
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 that 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 the URI, then Web API Framework returns a 404 error to the client who actually makes the request. For example, the following URIs match with the default route template
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
Here are some possible HTTP requests, along with the action that gets invoked for each request.
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.
Summary:
I Hope this post will be helpful to understand the concept of routing in Web API
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
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 the 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 that contains action methods that 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.
The above route is defined in the WebApiConfig.cs file, which is present inside the App_Start folder.
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 that 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 the URI, then Web API Framework returns a 404 error to the client who actually makes the request. For example, the following URIs match with the default route template
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
Here are some possible HTTP requests, along with the action that gets invoked for each request.
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.
Summary:
I Hope this post will be helpful to understand the concept of routing in Web API
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
0 comments:
Post a Comment
If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.