Sunday, October 17, 2021

ASP.NET Core 5.0 Web API

 

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; }
    }
}
C#

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);
    }
}
C#

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);
        }
    }
}
C#

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);
        }
    }
}
C#

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

ASP.NET CORE

 hOME

Saturday, October 16, 2021

ASP.NET Core CRUD Operations With Paging, Sorting, Searching, and Export Data Option

 Introduction

In this article, I will illustrate how to create an APS.NET core MVC web application using EF core. The main features of this application create a CRUD operation, faster Paging, Sorting, Searching, and Export Data to CSV, PDF, Copy to Clipboard, and print data as well.

After finishing this article you will learn, how to create a complete AP.NET Core web application with EF core and Generic Repository Pattern. The major advantage of this application is faster paging, sorting, filtering operation by implementing jQuery Data tables on the front end side.

Prerequisites

01. Visual Studio 2017
02. Install .NET Core 2.0.0 or above SDK
03. MSSQL Server 2008 or above

Technology I Used

01. ASP.NET Core
02. C#
03. Generic Repository Pattern
04. ASP.NET build in Dependency Injection
05. EF Core
06. LINQ
07. Razor Tag Helpers
08. jQuery Datatable
09. jQuery UI
10. Sweetalert 2
11. Bootstrap
12. REST API

Steps to Creating this Project

01. Open VS 2017 and Create an ASP.NET Core web application in Visual Studio 2017

02. Select MVC Project Structure from VS Template Project

03. And also create an ASP.NET Core class library project for EF Core data access layer. Here I have used the generic repository pattern for application data access.

04. Overall Project Structure

Create MOC Data in MSSQL Database

Using the following SQL script I have created 5 luck data for testing application data load, search and pagination performance. jQuery data tables render data very fastly from the server side by paging.

Create a Model Class:

DB Connection: appsettings.json

Startup.cs

Create data: Ajax Request

Data tables: Javascript code

File location in the project:
~Sln.jQueryDatatables\jQueryDatatables\wwwroot\js\PersonalInfo\PersonalInfo_Datatable.js

Data tables: C# Code

Export All Data to CSV

For export, all data I have used CSV helper. From Nuget library just install CsvHelper by following command in PMC,
Install-Package CsvHelper.

Conclusion

This is a very basic CRUD application using ASP.NET .NET core but advance in data operation. The application performs a faster data loading operation, which is implemented by jQuery data tables. Application successfully loaded 5 luck dummy data with paging within a few seconds. Searching, filtering, and paging are pretty fast as well. For future work, I will implement the login module in this project.

Thanks for your valuable time. I hope you fully understand and enjoyed my article. For further more query please email me at teamechno360@gmail.com