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 is 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 that 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.

API FLOW

Now the next question is: "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,

WebAPi

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 interacts with the business layer and gets Data from DB.
  3. The output will be returned in JSON format.

This is a 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.

Get Started

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

CreateNewProject

Provide the Project name and location.

Configure

Select Target Framework and click on the Create button.

Additional Info

Member API is created. See below project structure.

SolutionExplorer

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

Swagger

By default, Weather Api executes and displays output using Swagger. I hope you are aware of Swagger. 

Simply put, Swagger is an 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 Verbs

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

Traverse

First, we will create a Member Data Layer and then create a Member API Controller.

Step 1. Create a New .Net Class Library Called Member. Data

Step 2. Add three Folders; Models, Interface, and Repository in Member. Data Class library.

Model

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 a 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 a Class Called “MembersRepository” and implement the 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 this,

Repository

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

Repository

Step 8. Click on the Add button.

Add NEw Item

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

member data

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.

curl response

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

response body

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

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