Introduction of ASP.NET Web API :
ASP.NET Web API is a framework for building an easy HTTP web services that reaches the clients, including browser, mobile apps and so on.
Web API is a programming interface that provides a communication between the software applications. It provides the interface for websites and client application to have a data access. It can also access data from the database and can save the data back to the same database.
HTTP Methods:
For building a Api the most commonly use methods are:
1)GET – It will provides a read alone process .
2)PUT – It will provide you to create a new resource .
3)DELETE – It will remove the resources that you wanted to remove.
4)POST – It will provide you to update the resource .
Prerequesites:
1) Visual Studio Community 2022 :
https://visualstudio.microsoft.com/vs/community/
2)SQL Server Management Studio:
https://aka.ms/ssmsfullsetup
3)SQL Server:
https://www.microsoft.com/en-us/sql-server/sql-server-downloads
Let’s get started working with the Asp.Net Web API Projects in Visual Studio Community Edition 2022:
⦁ Open VS 2022 and Click Create a new project –> select the project template as ASP.NET Core Web API –> Add a name to the new project and click next –> Check the additional information whether it is in .NET 6.0 and then click create.
⦁ Install the packages from the Nuget package manager Go to Tools –> click Nuget Package Manger –> click Manage Nuget Packages for Solution.
Install the below packages:
1) Entity Framework
2)Microsoft.Entity.FrameworkCore
3)Microsoft.Entity.FrameworkCore.Design
4)Microsoft.Entity.FrameworkCore.Tools
5)Microsoft.Entity.FrameworkCore.SQLServer
if wanted you can also install Microsoft.extension.Confirugation
⦁ After installing move to Solution Explorer and create a new folder as Models. After creating model right click the model folder —>Add –> select the Class. Assume a class name.(Student).
⦁ In the class student add the attritubes with data annotations.
using System.ComponentModel.DataAnnotations;
namespace StudentDetails.Models
{
public class Student
{
[Key]
public int StudentId { get; set; }
[Required]
public string? StudentName { get; set;}
[Required]
public string? StudentAge { get; set;}
[Required]
public string? StudentPhoneNo { get; set; }
}
}
⦁ Selct the controller folder from the solution explorer, right click the controller —>Add —> click the Controller .
⦁ After clicking it, select the API option —-> Select API Controller with actions,using entity framework and click Add.
⦁ In the Add API Controller with actions, using Entity Framework dialog
–> Select your Model Class name (Student(StudentDetails.Models)) .
–> Add your DbContext name clicking the plus symbol (StudentDetails.Data.StudemntDetailsContext) and click Add.
–> Create a controller name and click Add .
⦁ After Clicking the add ,the controller will be generated and the db context also will be generated automatically in the project.
⦁ Open Appsetting.json from the solution Explorer, Check whether the ConnectionString is implemented.
⦁ In the connection String change the server name as your local database name.
Note:
If error occurs while migrating we have to add TrustServiceCertificate = True in the connection string.
⦁ Open the Package Manger Console(PMC) by (Tools –>Nuget Package Manger –> Package Manger Console.)and the command are mentioned below
- Add-Migration InitialCreate
- Update-Database
⦁ After updating the database ,Open Sql Server Management Studio(SSMS) and check whether the database for you project is created or not ,and check the table created in the sql.
⦁ Now move to Visual Studio 2022 and Excute the project ,the API screen will be opened with you controller name .In that you can see the HTTP method in the below.
Conclusion:
Hence we have created the Web API using Code First Approach .Other detailed concepts of Web API will be covered in the upcoming blogs.
No Comment! Be the first one.