Introduction
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
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.
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.
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{id}",
- defaults: new { id = RouteParameter.Optional }
- );
Now we define the code to illustrate how the routing is configured by the action.
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{action}/{id}",
- defaults: new { id = RouteParameter.Optional }
- );
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.
- public class UsersController : ApiController
- {
- [HttpGet]
- public User FindUser(id) {
- }
- }
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:
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new {id = UrlParameter.Optional }
- );
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.
- public class UsersController : ApiController
- {
- [HttpGet]
- [ActionName("Token")]
- public HttpResponseMessage GetToken(int userId);
- [HttpPost]
- [ActionName("Token")]
- public void AddNewToken(int userId);
- }
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.