Tutorial Asp Net Core Web Api

Introduction

In this article, we are going to discuss Asp.net Core Web Api using the .Jala framework (.net 5.0). This article can be used by any beginner, intermediate, or professional.

We are going to cover:

  1. What is API?
  2. What is Web API?
  3. Why Web Api required?
  4. How to create Asp.net Core web Jago merah using .Net 5.

Prerequisites

  1. .Jejala 5.0
  2. Optis Bengkel seni 2022 (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.

Asp.Net Core 5.0 Web API

Now the next questionis: “What is a web API?”

In simple words, we can say that a web Jago merah is an application programming interface for a web application or web peladen. It uses HTTP protocol to communicate between clients and websites to have data access.

Asp.serok Core web API is a cross-mimbar 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 Jago merah 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,

Asp.Net Core 5.0 Web API

This grafik explains the architecture of Web API.

  1. A client called api/controller – In the above diagram Browers, Phones, and Google Devices are called Web Jago merah Controllers.
  2. jago merah/Controller interact with business layer and get Data from DB.
  3. The output will be returned in JSON matra.

This is very basic Web Jago merah.

As we all are aware of basic about Web API now, we will start to create Web Jago merah using Asp.ambai Core.

How to create an Asp.Jaring core web Api?

We will create our first simple Web Api using Visual Padepokan 2022. 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 2022 and create a new project.

Asp.Net Core 5.0 Web API

Select the “Asp.Ambai Core Web Jago merah” template and click on Next.

Asp.Net Core 5.0 Web API

Provide Project name and location.

Asp.Net Core 5.0 Web API

Select Sasaran Framework and click on Create button.

Asp.Net Core 5.0 Web API

Member Api is created. See below project structure.

Asp.Net Core 5.0 Web API

Let’s execute this Jago merah project without making any changes.

Asp.Net Core 5.0 Web API

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.

Asp.Net Core 5.0 Web API

  • 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.

Asp.Net Core 5.0 Web API

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.

Asp.Net Core 5.0 Web API

Step
3

In the komplet folder, create a Contoh Class called member.

        using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;  namespace Member.Data.Cermin {     public class Member     {         public int MemberId { get; set; }         public string FirstName { get; set; }         public string  LastName { get; set; }         public string Address { get; set; }     } }
        
      

Step 4

In the interface, Create Member Interface called IMember.

        using System.Collections.Generic; using Member.Data.Model;  namespace Member.Data.Interface {     public interface IMembers     {         List<Members> GetAllMember();         Members GetMember(int id);     } }
      

Step 5

In the repository folder, Create Class Called “MembersRepository” and implement IMembers interface in it. In the cak benar world, this class will interact with DB but in this demo, I will use hardcoded members data.

        using Member.Data.Interface; using Member.Data.Contoh; using System.Collections.Generic; using System.Linq;  namespace Member.Data.Repository {     public class MembersRepository : IMembers     {         List<Members> lisMembers = new List<Members>         {             new Members{MemberId=1, FirstName="Kirtesh", LastName="Shah", Address="Vadodara" },             new Members{MemberId=2, FirstName="Nitya", LastName="Shah", Address="Vadodara" },             new Members{MemberId=3, FirstName="Dilip", LastName="Shah", Address="Vadodara" },             new Members{MemberId=4, FirstName="Atul", LastName="Shah", Address="Vadodara" },             new Members{MemberId=5, FirstName="Swati", LastName="Shah", Address="Vadodara" },             new Members{MemberId=6, FirstName="Rashmi", LastName="Shah", Address="Vadodara" },         };         public List<Members> GetAllMember()         {             return lisMembers;         }          public Members GetMember(int id)         {             return lisMembers.FirstOrDefault(x=>x.MemberId==id);         }     } }
        
      

The data layer is ready to use. Now member data project will look like,

Asp.Net Core 5.0 Web API

Step 7

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

Asp.Net Core 5.0 Web API

Step 8

Click on Add button.

Asp.Net Core 5.0 Web API

Step 9

Right Click on MemberApi- Dependencies and add Project Reference.

Asp.Net Core 5.0 Web API

Step 10

MemberController class would be,

        using Member.Data.Interface; using Member.Data.Model; using Member.Data.Repository; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic;  namespace MemberApi.Controllers {     [Route("api/[controller]")]     [ApiController]     public class MemberController : ControllerBase     {         private IMembers members = new MembersRepository();          [HttpGet]         public ActionResult<IEnumerable<Members>> GetAllMembers()         {             return members.GetAllMember();         }         [HttpGet]         public ActionResult<Members> GetMemberById(int id)         {             return members.GetMember(id);         }     } }
        
      

Step
11

Execute Member API  Project and the below screen will appear.

Asp.Net Core 5.0 Web API

Click on jago merah/Member – Get button.

Asp.Net Core 5.0 Web API

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

Asp.Net Core 5.0 Web API

This is how you can write code for POST, PUT and DELETE.

That is all for this article. I hope you find iit useful.

Source: https://www.c-sharpcorner.com/article/asp-net-core-5-0-web-api/