how to create  Web API in .Net core 3.1 MVC

how to create Web API in .Net core 3.1 MVC

In this article, you will see how to create Web API in .Net core 3.1 MVC. This article will help beginners who are new to API concepts. Let's explore creating web API in .Net core 3.1 MVC using a simple example.

Step 1: Create Web API Project

Create new web API project, New project--> Choose ASP.Net Core Web Application--> Choose API--> Click Create.

how to create  Web API in .Net core 3.1 MVC

Step 2:Create Database

write the following code in sql server query window for creating database and table.

 Create database Learn;
 Use Learn;
CREATE TABLE [dbo].[UserMaster](
 [UserID] [bigint] IDENTITY(1,1) PRIMARY KEY ,
 [UserName] [nvarchar](150) NULL,
 [Passward] [nvarchar](100) NULL,
 [Email] [nvarchar](100) NULL,
 [Dob] [datetime] NULL,
 [Gender] [nvarchar](50) NULL,
 [MobileNo] [nvarchar](100) NULL,
 [Location] [nvarchar](500) NULL,
 [IsActive] [bit] NULL,

Step 3: Install NuGet packages.

We need to install the below packages:


Step 4: Scaffold-DbContext Command

Use Scaffold-DbContext to create a model based on your existing database. In Visual Studio, select menu Tools -> NuGet Package Manger -> Package Manger Console and run the following command:

PM>Scaffold-DbContext'Server=.\SQLExpress;Database=Learn;Trusted_Connection=True;' Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

Step 4:Add Controller

Right-click on the directory where the controller should be created, the Controllers directory in the example, and select Add and then Controller.

how to create  Web API in .Net core 3.1 MVC

On the dialog that pops up, we want to select API Controller and then click Add

how to create  Web API in .Net core 3.1 MVC

Step 5 : UserController

Write the following code in UserController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DotNetCoreAngular.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace DotNetCoreAngular.Controllers
    public class UserController : ControllerBase
        readonly LearnContext _context;
        public UserController(LearnContext context)
            _context = context;
        public IEnumerable<UserMaster> Get()
            var data = _context.UserMaster.ToList();
            return data;
        public IActionResult Post([FromBody] UserMaster obj)
            var data = _context.UserMaster.Add(obj);
            return Ok();
        public IActionResult Put(int id, [FromBody] UserMaster obj)
            var data = _context.UserMaster.Update(obj);
            return Ok();
        public IActionResult Delete(int id)
            var data = _context.UserMaster.Where(a => a.UserId == id).FirstOrDefault();
            return Ok();


Here, we created a simple web api application that uses entityframe work,Asp.Net Core 3.1 preview, SQL Server, and Visual Studio 2019 to perform CRUD operations. You can leave the feedback/comment/questions about this article below. Please let me know how you like and understand this article and how I could improve it.

Aditya Patel

Aditya Patel

Hi, I am a Senior Software Developer in MindStick Software pvt. Ltd. I have done BCA from dr. c.v. Raman university. I have more than five years experience in Microsoft Technology MVC 4, MVC Core, C#,Vb.Net, rdlc Reporting ,SQL Server and other jQuery, css, Bootstrap, Knockout.js, angular.js,react.js Html, Crystal Reporting etc.


Leave Comment