ASP NET Core Web API |
Introduction
Here in this blog Create Web API in ASP.NET Core, we will demonstrate how to create Web API in ASP.NET Core using Microsoft Visual Studio 2022. Developing an ASP NET Core Web API is not a difficult part it is as easy as .Net Framework API.
Getting Started
Here we will develop a basic dotnet core web API with controllers, the following things we will learn here.
- Create a web API project.
- Add a model class.
- Scaffold a controller with CRUD methods.
- Configure routing, URL paths, and return values.
- Call the web API with http-repl.
Create Web API Project
First step is we will create the template of dotnet core Web API project. The following steps helps to create Web API.
- Open Microsoft Visual Studio.
- Click on Create a new Project.
- Enter Web API in the search box
- Select the ASP.NET Core Web API template and select Next.
- In the Configure your new project dialog, name the project StudentManager and select Next.
- In the Additional information dialog:
- Confirm the Framework is .NET 6.0 (Long-term support).
- Confirm the checkbox for Use controllers (uncheck to use minimal APIs) is checked.
- Select Create.
Add Model to Project
Now we are done with the Web API template creation, the next step is to add model class in API. The following steps helps to add model call to project.
- In Solution Explorer, right-click the project.
- Select Add > New Folder. Name the folder Models.
- Right-click the Models folder and select Add > Class.
- Name the class StudentModel and select Add.
- Replace the template code with the following:
namespace HelloWorldAPI.Models { public class StudentModel { public int Id { get; set; } public string? Name { get; set; } public string ClassName { get; set; } } }
Add Controller to Project
- Right-click the Controllers folder.
- Select Add > New Scaffolded Item.
- Select API -Empty, and then select Add.
- Rename the ValuesController.cs to StudentController.cs
- Select Add.
- Replace the template code with the following:
using HelloWorldAPI.Models; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace HelloWorldAPI.Controllers { [Route("api/Student")] [ApiController] public class StudentController : ControllerBase { [HttpGet(Name = "GetStudents")] public ActionResult<List<StudentModel>> GetStudents() { return this.Students(); } //[HttpGet(Name = "StudentList")] //public ActionResult<List<StudentModel>> StudentlList() //{ // return this.Students(); //} [HttpPost] public ActionResult<List<StudentModel>> NewStudent(StudentModel student) { List<StudentModel> students = this.Students(); students.Add(student); return students; } private List<StudentModel> Students() { List<StudentModel> students = new List<StudentModel>(); students.Add(new StudentModel { Id = 1, Name = "Kailash",ClassName="I" }); students.Add(new StudentModel { Id = 2, Name = "Nidhi", ClassName = "I" }); students.Add(new StudentModel { Id = 3, Name = "Mini", ClassName = "I" }); students.Add(new StudentModel { Id = 4, Name = "Ani", ClassName = "I" }); students.Add(new StudentModel { Id = 5, Name = "Puja", ClassName = "I" }); students.Add(new StudentModel { Id = 6, Name = "Kalyani", ClassName = "I" }); students.Add(new StudentModel { Id = 7, Name = "Krishna", ClassName = "I" }); students.Add(new StudentModel { Id = 8, Name = "Nila", ClassName = "I" }); return students; } } }
The above demonstration creates following two GetStudents and NewStudent which will work as your APIs.
Methods- GetStudents : returns list of students
- NewStudetn : Add the student details into students list and returns list of students with including the new student details.
- Get/api/Student/GetStudents : calls the GetStudents funcation of controller class
- Post/api/Student/NewStudent: Calls the Newstudent function of controller class
Build and run the source code. Here swagger is enabled when your code successfully runs, you will see swagger window on browser like below image.
Testing Web API
As I mentioned in the above paragraph, the swagger is enabled here. Third parti tool is not required to test the Web API. On the swagger window expand the Post API(Post/api/Student). When you expand the api the swagger will appear like below image.
Click on the ‘Try it out’ button, swagger will allow you to edit the request body. Change the property value like below and click on the ‘Execute’ button.
{
"id": 0,
"name": "Nidhika",
"className": "B"
}
The above API will process your request and will add the new student details that you have provided in the request body. Then the API will give you the list of students including the new student as response.
Summary
In the demonstrator, I hope the steps and c# webapi example helped you to learn how to create Web API in ASP.NET Core and test the .
Thanks