In this project, we'll use:-
· ".Net Core SDK" (Version 3.0)· Visual Studio Code
· SQL Server Management 2012
· Postman (for test API)
How to Create Web API project:
Step 1: Firstly, open Visual Studio Code Terminal and Enter this Command to create a new directory and in this directory create a new Web API project:mkdir StudentAPI (create project name directory)
cd StudentAPI (change the current directory)
dotnet new webapi (create webapi project)
After that, Run the Application using this command :
cd StudentAPI (change the current directory)
dotnet new webapi (create webapi project)
![]() |
Web API new Project Create through visual studio code terminal |
dotnet run
Check the output of the application in the browser by type the URL :
https://localhost:5001/weatherforecast
![]() |
Demo Project run |
Step 2: Now, install some tools and packages in the project.
Firstly, we will install Tools:
dotnet tool install --global dotnet-ef --version 3.0.0-*
dotnet tool install -g dotnet-aspnet-codegenerator
dotnet tool install -g dotnet-aspnet-codegenerator
Second, Add NuGet Packages by using the following command :
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer.Design
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools
We can show all NuGet packages in the StudentAPI.csproj file
dotnet add package Microsoft.EntityFrameworkCore.SqlServer.Design
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools
We can show all NuGet packages in the StudentAPI.csproj file
![]() |
StudentAPI.csproj file |
But make sure this all package versions should support the Targeted Framework (netcoreapp3.0 >= package version).
Step 3: Now, we need to create a "Models" folder and add the following file :
· Student.cs
Student. cs will contain all code related to Student Record.
Step 4: Now add ConnectionString in appsettings.json file and connectionString looks like this :
"ConnectionStrings": { "DefaultConnection": "Data Source=Server Name; Initial Catalog=Database Name; Integrated Security=True; Connect Timeout=15; Encrypt=False; TrustServerCertificate=False" }
Step 5: Create a new folder called "Data" and in this folder, we need to create StudentContext.cs file.
DbContext is a bridge between your domain or entity classes and the database. DbContext is the primary class that is responsible for interacting with the database
.StudentContext file will contain information for the database.
Step 6: Now in Startup. cs file add the following code:- using StudentAPI.Data;
using StudentAPI.Models;
using Microsoft.EntityFrameworkCore;
(this three file add in header)
and in the "ConfigureServices" method add this dependency injection :
Step 7: Now perform migration and update the database to migrations.
enter following code in the terminal for migration :
dotnet ef migrations add FirstMigration
FirstMigration is a migration name, we can write any migration name.
dotnet ef database update
We can see the database has been updated in SQL Server.
dotnet aspnet-codegenerator controller -name StudentController -async -api -m Student -dc StudentContext -outDir Controllers
We can see that this code create StudentController in Controllers folder.
this code creates four operations of the Web API application like:
· HttpGet
· HttpPut
· HttpPost
· HttpDelete
(means Display details, Update, Create, and Delete respectively)
Step 9: First, Run our application using dotnet run command,
then open the postman software to check the API call.
In this tool, first, select the return type method to GET and after entering the following URL in the textbox :
· https://localhost:5001/api/student and click to SEND button.
By using this we can get all students to detail in JSON format.
Next, in the HttpPost method, select the POST method and enter the same URL, and enter the Input Json into a body part, and click to send button.
· URL: https://localhost:5001/api/student
· body part : {"name" : "XYZ", "semester" : "5th", "department" : "Computer Engineer"}
in the delete method, there is no requirement to define input in the body part.
When we call the HttpPut method then that time we need to define ID in both URLs as well as a body part.
Tags:
.net core
API App in asp
asp
asp.net core
c#
csharp
dotnet
DotNet Core
web api
web api authentication
web api security
web mvc app