The term API is a buzzword among developers and tech enthusiasts alike. This shot discusses an API with regards to the ASP.NET
framework and a focus on ASP.NET
’s Web API framework.
API stands for Application Programming Interface. It is a set of programming codes that enable the transfer of data from one application to another. An API facilitates communication among software applications.
A simple example is Google’s location API, which provides drivers with information on the time required to travel across destinations. Another example is the Yelp API, which provides recommendations on the best restaurants, nightlife, etc.
Ideally, every API has a document that specifies instructions about how to integrate with an API. This document is called API Documentation.
Web API is an API over the web.
ASP.NET
ASP.NET
is an open-source web framework. It is a part of the .Net platform, used to produce interactive, data-driven web applications over the internet.
ASP.NET
Web API is the ASP.NET
framework used to build HTTP services.
The difference between ASP.NET
Web API and ASP.NET
MVC web application is that ASP.NET
Web AP sends its responses in data format instead of an HTML view. SP.NET
Web AP is like a web service, so it only supports HTTP protocol.
Some of its features include:
ASP.NET
platform for building RESTful services.ASP.NET
framework, and supports the ASP.NET
request/response pipeline.ASP.NET
Web API Framework work?The Web API uses the HTTP protocol. HTTP works as a request-response protocol between a client and server. A client, in the form of a browser, sends an HTTP request to the server. The server then returns an appropriate response to the client.
In ASP.NET
, the class that handles HTTP requests is called a controller. When the framework receives a request, it routes the request to an action. It then uses the routing table to determine which action/verb to invoke for each HTTP request. The routing parameters are in the webApiConfig.cs
file in the App_Start directory.
For instance, if we have an API that retrieves a list of all the banks in Lagos, Nigeria, the initial webApiConfig.cs file would look like:
config.Routes.MapHttpRoute(name: "BankListApi",routeTemplate: api/{controller}/{id}",defaults: new { id = RouteParameter.Optional});
When the code to illustrate how the routing is configured by the action is defined, the file would look like:
config.Routes.MapHttpRoute(name: "BankListApi",routeTemplate: api/{controller}/{action}/{id}",defaults: new { id = RouteParameter.Optional});
A Uniform Resource Identifier (URI) is a sequence of characters that distinguish one resource from another. When the Web API Framework receives an HTTP request, it matches the URI against one of the route templates in the routing table. If no route matches, the client will get a 404 error (source: Routing in ASP.NET
Web API, Mike Wasson, 2018).
The table below illustrates a sample of a routing table:
HTTP Verb | URI | Action | Parameter |
---|---|---|---|
GET | api/banks | GetAllBanks | None |
GET | api/banks/1 | GetBankByID | 1 |
POST | api/bank | ||
Delete | api/banks/1 | Delete Bank | 1 |
Take a look at this list of HTTP verbs below:
Sources: Mike Wasson, 2018. Routing in
ASP.NET
Web API https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api