How ASP.Net Web API Works


This article explains how the Web API works. Here you will see the application layer of the API, the MVC architecture and the Web API architecture.

Application layer

The Web API is a framework for building web services, these web services use the HTTP protocol. The Web API returns the data on request from the client, and it can be in the format XML or JSON.

Application Architecture

application Layer

MVC architecture

The MVC architecture is the Model-View-Controller Pattern. It is used for isolating an application to simplify testing and the maintenance of the code of the application.

Model: Model is used for the data of the application and the objects of the business.
View: View is the presentation layer of the application.
Controller: Controller works for binding the Model and View together.

MVC architecture

Web API Architecture

The Web API uses the HTTP protocol. The Web API routing works a bit differently compared to the way that it works in MVC. There is a minor difference between the MVC routing and Web API routing, In the Web API the selection of the action uses the HTTP method instead of the URI path. It uses the Routing table that defines the action to be called for which request. We need to specify the routing parameters in the webApiConfig.cs file.

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

Now we define the code to illustrate how the routing is configured by the action.

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

The following table describes the routing.

HTTP Method URI Action Parameter
GET api/users GetAllUser None
GET api/users/1 GetUserByID 1
POST api/users    
Delete api/users/1 Delete User  1

The web API Framework matches the segments in the URI path to the template. The following is the procedure performed.

  • First the URI matches the route template.
  • Second is the related controller selected.
  • Third is the selection of the related action.

There is a method that selects the controller, the method is IHttpControllerSelector.SelectController. It takes an instance of HttpRequestMessage and then returns the HttpControllerMethod. After the selection of the controller the Web API selects the action by calling the  IHttpActionSelector.SelectAction.

  1. public class UsersController : ApiController  
  2. {  
  3.     [HttpGet]  
  4.     public User FindUser(id) {  
  5.     }  
  6. }

We can also use another HTTP method, such as POST, PUT or DELETE.

You can also add the action name in the route; see this code:

  1. routes.MapRoute(  
  2.                 name: "Default",  
  3.                 url: "{controller}/{action}/{id}",  
  4.                 defaults: new {id = UrlParameter.Optional }  
  5.             );  

If you want to override the Action Name then that is possible using the Action Name Attribute.There is an example that defines the two actions, one supports GET and another supports POST.

  1. public class UsersController : ApiController  
  2. {  
  3.     [HttpGet]  
  4.     [ActionName("Token")]  
  5.     public HttpResponseMessage GetToken(int userId);  
  6.     [HttpPost]  
  7.     [ActionName("Token")]  
  8.     public void AddNewToken(int userId);  
  9. } 

In the Web API framework, the controller manages the HTTP request. The Controller includes the collection of the action methods. The incoming request to the Web API framework, the request matches to the appropriate action.