• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Friday, April 24, 2020

ASP.NET Web API Routing

 Admin     April 24, 2020     .Net, Web API     No comments   

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.
Routing in ASP.NET Web API

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

  • /api/student
  • /api/students/1
  • /api/products/prd1
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.

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 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • C# Programming Examples on Sorting
    Today i am going to tell you some of the Sorting programming questions in C#. Q1- Write a C# program to perform Selection sort. Ans:  Sel...
  • Calling Web API Service in a Cross-Domain Using jQuery AJAX
    In this article, I am going to discuss Calling Web API Service in a Cross-Domain Using jQuery AJAX . Please read our previous article befor...
  • ViewBag in ASP.NET Core MVC
    In this article, I am going to discuss the use of ViewBag in ASP.NET Core MVC application with examples. Please read our previous article ...
  • Recursion And Back Tracking
    In this article, I am going to discuss Recursion And BackTracking in detail. Please read our previous article where we discussed Master Th...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • Binary to Decimal Conversion in C# with Examples
    In this article, I am going to discuss the Binary to Decimal Conversion in C# with some examples. Please read our previous article where w...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape