Introduction
In this article, we are going to discuss Asp.net Core Web Api using the .Net framework (.net 5.0). This article can be used by any beginner, intermediate, or professional.
We are going to cover:
- What is API?
- What is Web API?
- Why Web Api required?
- How to create Asp.net Core web API using .Net 5.
Prerequisites
- .NET 5.0
- Visual Studio 2019 (V 16.8 or later)
What is Web API?
The first question comes to mind is, "What is API”?
API stands for Application Programming Interface. It is an intermediate software agent that allows two or more applications to interact with each other.

Now the next questionis: "What is a web API?"
In simple words, we can say that a web API is an application programming interface for a web application or web server. It uses HTTP protocol to communicate between clients and websites to have data access.
Asp.net Core web API is a cross-platform web API.
Why is Web API required?
The user wants to access the application from different devices like mobile, browser, Google devices, etc. In this case, Web API can be useful.
Different devices request to Web API and Web API will respond in JSON format. Most of the devices are able to understand JSON output.
Let’s see the below web Api Architecture diagram,

This diagram explains the architecture of Web API.
- A client called api/controller – In the above diagram Browers, Phones, and Google Devices are called Web API Controllers.
- api/Controller interact with business layer and get Data from DB.
- The output will be returned in JSON format.
This is very basic Web API.
As we all are aware of basic about Web API now, we will start to create Web API using Asp.net Core.
How to create an Asp.Net core web API?
We will create our first simple Web API using Visual Studio 2019. You can download the free community version from the Microsoft official site.
Follow the below steps to create your first Web API project,
Open Visual Studio 2019 and create a new project.

Select the “Asp.Net Core Web API” template and click on Next.

Provide Project name and location.

Select Target Framework and click on Create button.

Member API is created. See below project structure.

Let's execute this API project without making any changes.

By Default Weather Api executed and displays output using Swagger. I hope you are aware of Swagger.
Simply put, Swagger is open-source software tool to design, build, document, and use RESTful Web API.
Web API is mostly used for CRED (Create, Read, EDIT, DELETE) operations. It follows HTTP verbs for these operations.

- HTTP GET – Read Operation
- HTTP POST – Create Operation
- HTTP PUT – Update Operation
- HTTP DELETE – Delete Operation
Following Diagram will explain our project which we are going to create in this article.

First, we will create Member Data Layer and then create Member API Controller.
Step 1
Create New .Net Class Library Called Member. Data
Step 2
Add three Folders; Models, Interface, and Repository in Member. Data Class library.

Step 3
In the model folder, create a Model Class called member.
Step 4
In the interface, Create Member Interface called IMember.
Step 5
In the repository folder, Create Class Called “MembersRepository” and implement IMembers interface in it. In the real world, this class will interact with DB but in this demo, I will use hardcoded members data.
The data layer is ready to use. Now member data project will look like,

Step 7
Now we will create MembeApi Controller in the Controller folder. Right-click on the Controller folder and Click on Add-Controller

Step 8
Click on Add button.

Step 9
Right Click on MemberApi- Dependencies and add Project Reference.

Step 10
MemberController class would be,
Step 11
Execute Member API Project and the below screen will appear.

Click on api/Member – Get button.

Now will try /api/Member/{id} with id =1

No comments:
Post a Comment