What is ASP.NET API?

Introduction

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.

What is an API?

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:

  • It is an ideal ASP.NET platform for building RESTful services.
  • It is built on the ASP.NET framework, and supports the ASP.NET request/response pipeline.
  • It maps HTTP verbs to methods names.
  • It supports different formats of response data together with JSON and XML.

How does the 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:

  • HTTP Get: gets information or data from a database or other data source.
  • HTTP POST: creates a new record with information or data in a data source.
  • HTTP PUT: updates an existing record in a database with information.
  • HTTP DELETE: deletes existing entries within a database or other source.
  • HTTP PATCH: updates an existing entry in a database with partial data.

Sources: Mike Wasson, 2018. Routing in ASP.NET Web APIhttps://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

Free Resources