Section 2: Basics of API (Restful API in ASP .Net Core)

 Section 2: Basics of API 

What is an API?


API is a piece of software that another software can use. This is more kind of academic definition. To give you a real-time example, software such as SkyScanner lets you to search for flights. It lists down all flights available from various airlines. How can SkyScanner know that data? The answer is that Airlines will develop an API and SkyScanner will use those APIs to get the data. 


Some Basics of API .Net Core.


We will create a API in this course, and another software will use the APIs. To test our APIs, we will use Postman

When a request is received to the server (where our API is deployed), how are they processed?

To understand this, let us see what are the main components of .Net Core API.

The major components in ASP .Net are:

  • Model
  • Controller
  • Views (Since we are developing API, we dont have View here). But in MVC, we will also have View. 
Model is a class, and the model class will be mapped to a table in the database. Suppose you want to store the data in RDBMS, and it has 5 tables, then we will have 5 Model classes; each class represents each table. 

Controller: When our application is deployed, any request coming to the server will be routed to the Controller. Controller will read the data from the Database and return the data to the calling software.

Middleware or Request Pipeline:


Before the request goes to the controller, the request have to go through a middleware or request pipeline. In middleware, configuration or setup request to process the request is done. For example, if we protect the API using Authentication and Authorization, we will do this in Middleware. Also note that the sequence of this configuration is very important in Middleware. 



How to create a ASP .Net Core project in Visual Studio?


Open Visual Studio and select ASP .NET and select ASP. Net Core Web API. Leave all settings as default. Please refer below for more details:




Project Structure


After you create the project, your solution explorer shows the below folder structure.

It contains:

Dependencies: All dependencies required to buidl this project are stored here. 

Controller: This folder contains code for the controller. We shall delete the default WeatherController.cs. We shall create our own controller. The term may confuse you, COntroller.We will see this soon in the next section.

appsettings.json: This file contains the configuration details required by our application. It includes (not limited to) username/password of our database, etc.