Create Web API in ASP.NET Core

Kailash Chandra Behera | Sunday, June 26, 2022
swagger configuration c# web api
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.

  1. Create a web API project.
  2. Add a model class.
  3. Scaffold a controller with CRUD methods.
  4. Configure routing, URL paths, and return values.
  5. 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.

  1. Open Microsoft Visual Studio.
  2. Click on Create a new Project.
  3. Enter Web API in the search box
  4. Select the ASP.NET Core Web API template and select Next.
  5. In the Configure your new project dialog, name the project StudentManager and select Next.
  6. In the Additional information dialog:
    1. Confirm the Framework is .NET 6.0 (Long-term support).
    2. Confirm the checkbox for Use controllers (uncheck to use minimal APIs) is checked.
    3. 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.

  1. In Solution Explorer, right-click the project.
  2. Select Add > New Folder. Name the folder Models.
  3. Right-click the Models folder and select Add > Class.
  4. Name the class StudentModel and select Add.
  5. 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

  1. Right-click the Controllers folder.
  2. Select Add > New Scaffolded Item.
  3. Select API -Empty, and then select Add.
  4. Rename the ValuesController.cs to StudentController.cs
  5. Select Add.
  6. 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
  1. GetStudents : returns list of students
  2. NewStudetn : Add the student details into students list and returns list of students with including the new student details.
Urls
  1. Get/api/Student/GetStudents : calls the GetStudents funcation of controller class
  2. 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.

asp net core web api

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.

dotnet core web api

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.

c# webapi example
swagger in webapi

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


No comments: