ASP.NET Core 5.0 Web API

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:

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

Prerequisites

  1. .NET 5.0
  2. 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.

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 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,

Asp.Net Core 5.0 Web API

This diagram explains the architecture of Web API. 

  1. A client called api/controller – In the above diagram Browers, Phones, and Google Devices are called Web API Controllers.
  2. api/Controller interact with business layer and get Data from DB.
  3. 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.

Asp.Net Core 5.0 Web API

Select the “Asp.Net Core Web API” 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 Target 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 API 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 model folder, create a Model Class called member.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Member.Data.Model
{
    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 real world, this class will interact with DB but in this demo, I will use hardcoded members data.

using Member.Data.Interface;
using Member.Data.Model;
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 api/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.