articles

Home / DeveloperSection / Articles / how to create Web API in .Net core 3.1 MVC

how to create  Web API in .Net core 3.1 MVC

how to create Web API in .Net core 3.1 MVC

Anonymous User 2113 19-Oct-2020

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;
GO;
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,
)
GO

Step 3: Install NuGet packages.

We need to install the below packages:

Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.SqlServer.Design
Microsoft.EntityFrameworkCore.Tools

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
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        readonly LearnContext _context;
        public UserController(LearnContext context)
        {
            _context = context;
        } 
        [HttpGet]
        public IEnumerable<UserMaster> Get()
        {
            var data = _context.UserMaster.ToList();
            return data;
        }
        [HttpPost]
        public IActionResult Post([FromBody] UserMaster obj)
        {
            var data = _context.UserMaster.Add(obj);
            _context.SaveChanges();
            return Ok();
        }
        [HttpPut("{id}")]
        public IActionResult Put(int id, [FromBody] UserMaster obj)
        {
            var data = _context.UserMaster.Update(obj);
            _context.SaveChanges();
            return Ok();
        }
        [HttpDelete("{id}")]
        public IActionResult Delete(int id)
        {
            var data = _context.UserMaster.Where(a => a.UserId == id).FirstOrDefault();
            _context.UserMaster.Remove(data);
            _context.SaveChanges();
            return Ok();
        }
    }
}

Summary

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.


I am a content writter !

Leave Comment

Comments

Liked By