---
title: "how to create  Web API in .Net core 3.1 MVC"  
description: "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."  
author: "Anonymous User"  
published: 2020-10-19  
canonical: https://www.mindstick.com/articles/324352/how-to-create-web-api-in-dot-net-core-3-1-mvc  
category: "asp.net core"  
tags: ["api(s)", "asp.net web api", "asp.net core"]  
reading_time: 3 minutes  

---

# 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](https://www.mindstick.com/articles/85566/how-to-use-data-validation-in-mvc-core)--> Choose API--> Click Create.

![how to create  Web API in .Net core 3.1 MVC](https://www.mindstick.com/mindstickarticle/356090d7-1e5b-4a47-8e0d-e3cab7331afe/images/e0b8ce0b-f700-4e3c-aa02-9658608c2f29.png)\

## Step 2:Create Database

write the following code in [sql server](https://www.mindstick.com/articles/13006/subquery-in-sql-with-example) 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](https://answers.mindstick.com/qa/31181/some-nuget-packages-were-installed-using-a-target-framework-different-from-the-current-target-framework-and-may-need-to-be-reinstalled).**

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](https://www.mindstick.com/forum/159905/how-can-i-generate-migrations-for-an-existing-database-in-dot-net-6). In [Visual Studio](https://www.mindstick.com/forum/12863/how-to-insert-a-video-in-asp-dot-net-web-page-in-visual-studio-2010), select menu Tools -> [NuGet Package](https://www.mindstick.com/forum/155814/what-is-nuget-package-management) 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](https://www.mindstick.com/mindstickarticle/356090d7-1e5b-4a47-8e0d-e3cab7331afe/images/682eb11d-28be-49cd-9d05-084d956f4895.png)\

On the dialog that pops up, we want to select [API Controller](https://www.mindstick.com/forum/12899/how-can-i-extend-an-api-controller-to-hold-a-variable-for-my-database-context) and then click Add

![how to create  Web API in .Net core 3.1 MVC](https://www.mindstick.com/mindstickarticle/356090d7-1e5b-4a47-8e0d-e3cab7331afe/images/6e39167b-89d8-4bf1-a982-78d93e884d79.png)\

## 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](https://www.mindstick.com/forum/161611/how-do-you-save-user-uploaded-files-to-disk-in-an-asp-dot-net-mvc-or-web-api-application) that uses [entityframe](https://www.mindstick.com/articles/12153/how-to-access-data-in-mvc-using-entity-framework) work,Asp.Net Core 3.1 preview, SQL Server, and Visual Studio 2019 to perform [CRUD operations](https://www.mindstick.com/forum/160899/how-do-i-write-crud-operations-to-modify-data-in-sql-server-tables). 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.

---

Original Source: https://www.mindstick.com/articles/324352/how-to-create-web-api-in-dot-net-core-3-1-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
