Web API’s Basics
Hi everyone,
Today, I will cover the concept Web API and I will try to show you how to develop a simple Web API in ASP.NET Core 5.0.
Firstly, What Is API?
It is an acronym for Application Programming Interface. So, API is simply some kind of interface, which we can define as intermediary software, that allows other programmers to access an application’s feature, operating system or data of an application, etc.
The main idea behind the API is simplify the application development in multi-platform. For instance, if you want to develop a cross-platform mobile application, you need to use same services, features and data for each platform. However, developing same services for both platform only vastes your time and money. So, instead of duplicating codes, you can develop a single API for integrating new application into existing ones. Therefore, your applications can also be able to support rapid changes.
So, Web API is…
Web API is an API over the Web that user can be access using HTTP Requests. It provides communication convention-based CRUD operations with HTTP verbs GET, POST, PUT, DELETE. So, it can be efficient in the implementation of RESTFUL web services. Responses can be in different formats like XML, JSON. Web APIs can be self-hosted or hosted on IIS. Additionally, all Web Services are APIs, but not all APIs are Web Services. Because a Web Service facilitates interaction between two platforms over a network, but an API provides a direct communication between two apps.
Why should we use Web APIs?
- A Web API services are preferable over other services to use with a native application that does not support SOAP but require web services.
- For creating resource-oriented services, the web API services are the best to choose. By using HTTP or restful service, these services are established.
- If you want good performance and fast development of services, the web API services are very helpful.
- The devices that have tight bandwidth or having a limitation in bandwidth, then the Web API services are the best for those devices.
Let’s Create a Web API with Asp.Net Core 5.0…
First of all, let me show you the scenario of the application that we will develop:
We will create firstly the model class and create the properties for serializing our JSON format.
Then, we will create our context class for coordinating Entity Framework functionality (Read and Write) for our data.
And, we will create our APIs controller class with HTTP methods GET, POST, PUT and DELETE.
Finally, we can test our API via POSTMAN. Let’s start with coding.
First think is that you need to do create an ASP.NET Core Web Application and select the ASP.NET Core Web API template (Be sure about the version that I am using 5.0). If you click the create, then .Net template creates a WeatherForecast API with support for Swagger automatically. However, we do not need to use it. So you can delete the WeatherForecest.cs and remove the related part in the Startup.cs.
Now, we need to update our own launcher URL. So, we need to change “launchUrl” part inside of the launchSettings.json file as shown below:
You can see there are two launcher parts; one of them is “IIS Express” and other is “TodoApi”, the reason is that we mentioned before an API can be hosted in self and also can be hosted in IIS.
Now, we need to create our model and create properties for our JSON format.
Create a ToDoItem class inside of the Models folder (Create Models Folder if it does not exist). And create the properties given below:
Note that the Id property acts like a unique key in a relational database, so do not need to give an id each time while we are using Postman POST method.
Next, we need to install EntityFrameworkCore.SqlServer packages. In Visual Studio 2019, you can install it from Tools > NuGet Package Manager > Manage Nuget Packages for Solution. When we installed the package, now we can be able to give the EF Functionality for our data model inside the database context class.
If you installed it successfully, now add the ToDoContext class inside the Models folder. And inherit the class with DbContext class shown below:
Since we created the derived database context class from EntityFramework, now we need to register the database context services with the dependency injection container. So, it provides service to our controller class and specifies the database context will use an in-memory database. In order to do that, add the following lines inside the ConfigureServices method that inside the Startup.cs file:
Finally, we need to create our controller class for responding a request with the automatically created HTTP attributes uses dependency injection to inject database context into the controller and db context is used in each of the CRUD methods in the controller.
In order to create controller, in visual studio 2019 add New Scaffolded item with the API Controller with actions, using Entity Framework. And inside the dialog, select our model class for Model, select our context class for Data context.
Now api controller routing and GET, GET with ID, POST, PUT and DELETE methods created. And as you can see in the PostToDoItem method shown below the id, that I mentioned, assigned automatically when we create a new POST:
So, congratulations! You created your simple Web API that is in JSON format and has the properties id, name and isComplete with the HTTP attributes. If we run the program know we will see an empty JSON file:
Via Postman, we can POST data by creating a new request. So,
- firstly create a new request in Postman and select POST attribute,
- set the URL as “https://localhost:<port>/api/TodoItems” with your local host port number,
- select the body tab and select the raw radio button
- set the type as JSON
- and type the request body as shown and enter the request.
And congrats again! You have POST your first data via Postman. When you send a GET request again to the API, you can see your POSTed data.
To conclusion, we basically create a Web API which has HTTP attributes that provides ease of use to controlling JSON data. So, If we want to use this JSON formatted data in multiple king of platforms, only thing that we need to do is implementing the API inside the platform and that’s it. You can add new data via Postman also, so the JSON data can be updated automatically for every platform.