Creating A WEB API Project In Visual Studio 2019 - ASP.NET Core And Swagger

Introduction

 
Web APIs are simple Non-SOAP-based HTTP services.  We have client-side and server-side, when it comes to server-side, data stored on a database and WEB API acts as an intermediary that fetches data or inserts data to the DB. On the client-side, the actions take place on the user computer. To communicate between client-side and server-side we need REST Apis. ASP.Net Web API is a framework for building HTTP services. We can do different operations using APIs like update invoice details, Insert new user, delete customer order, etc.
  
Let's start creating a new ASP.Net Core WEB API project.
 
Step 1
 
Go to File =>  New Porject => Select ASP.NET Core Web Application.
                      
Creating WEB API project Visual Studio 2019 - ASP.NET Core And Swagger
 
Step 2
 
Add Project Name and Location. Click on Create.
 
Creating WEB API project Visual Studio 2019 - ASP.NET Core And Swagger
 
Step 3
 
From the dropdown, select .NET Core and select API. Click on Create.
 
Creating WEB API project Visual Studio 2019 - ASP.NET Core And Swagger
 
Step 4
 
After creating the project, it should look like the below image. Build and Run the project by clicking on IIS Express.
 
Creating WEB API project Visual Studio 2019 - ASP.NET Core And Swagger
 
Step 5
 
Visual Studio displays the following dialog: Select Yes if you trust the IIS Express SSL certificate. If you have given this permission earlier, these popups won't appear.
 
Creating WEB API project Visual Studio 2019 - ASP.NET Core And Swagger
 
The following dialog is displayed: Select Yes and continue.
 
Creating WEB API project Visual Studio 2019 - ASP.NET Core And Swagger
 
Step 6
 
By default, it loads JSON content on the browser.
 
Creating WEB API project Visual Studio 2019 - ASP.NET Core And Swagger
Step 7
 
Swagger is the best way to showcase WEB APIs. A Swagger object model exposes SwaggerDocument objects in JSON. After setting up the swagger we start creating an API controller.
 
We have to Install the package "Swashbuckle.AspNetCore". Right-click on your WebAPi project and click on Manage NuGet Packages.
 
Creating WEB API project Visual Studio 2019 - ASP.NET Core And Swagger
 
Creating WEB API project Visual Studio 2019 - ASP.NET Core And Swagger
 
The below popups will open. Click OK and Accept the License. 
 
Creating WEB API project Visual Studio 2019 - ASP.NET Core And Swagger
 
Creating WEB API project Visual Studio 2019 - ASP.NET Core And Swagger
 
After installing the package, update Startup.cs file to enable Swagger.
 
Insert this to the ConfigureServices function.
  1. // Register the Swagger generator, defining 1 or more Swagger documents  
  2.           services.AddSwaggerGen();  
Insert these lines to Configure function.
  1. // Enable middleware to serve generated Swagger as a JSON endpoint.  
  2.            // Enable middleware to serve generated Swagger as a JSON endpoint.  
  3.            app.UseSwagger(c =>  
  4.            {  
  5.                c.SerializeAsV2 = true;  
  6.            });  
  7.   
  8.            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),  
  9.            // specifying the Swagger JSON endpoint.  
  10.            app.UseSwaggerUI(c =>  
  11.            {  
  12.                c.SwaggerEndpoint("/swagger/v1/swagger.json""My API V1");  
  13.                c.RoutePrefix = string.Empty;  
  14.            });  
Step 8
 
Right-click on the Controller Folder as shown in the below image to create a new controller file. 
 
Creating WEB API project Visual Studio 2019 - ASP.NET Core And Swagger
 
Select any API Controller. If you select API Controller with read/write actions, by default it will create REST actions to create, read, update, delete APIs. 
  • API Controller - Empty will create an empty controller.
  • API Controller with actions, using Entity Framework will create an API controller with REST actions and list entities from Entity Framework data context. 
Creating WEB API project Visual Studio 2019 - ASP.NET Core And Swagger
 
Give any proper name to the controller.  
 
Creating WEB API project Visual Studio 2019 - ASP.NET Core And Swagger
 
UserController with a list of GET, POST, PUT, and DELETE APIs. 
 
[Route("api/[Controller ]")]  its a default routing system. here api/[controller] is api/name of the controller like api/User. 
 
The [ApiController] attribute makes model validation errors automatically trigger an HTTP 400 response.  
 
The controller class is derived from ControllerBase class which provides many properties and methods that are useful for handling HTTP requests. 
 
Creating WEB API project Visual Studio 2019 - ASP.NET Core And Swagger
 
Step 9
 
By default, launchUrl is set to  "api/value".  Make it an empty string so that it will load swagger with a list of controllers and corresponding APIs.
 
Creating WEB API project Visual Studio 2019 - ASP.NET Core And Swagger 
 
Step 10
 
Run the code by clicking on IIS Express. It will load Controllers with a list of APIs. 
 
You can see GET, POST, PUT, and DELETE with different colors.   
 
Creating WEB API project Visual Studio 2019 - ASP.NET Core And Swagger
 

Summary

 
This is the basic flow for creating a WEB API in ASP.NET Core. In the next part, we see how to write API and integrate databases. 


Similar Articles