How to create A WEB API Project In Visual Studio with ASP.NET Core And Swagger
What is Web APIs?
Web API as the name suggests, is an API over the web which can be accessed using HTTP protocol. It is a concept and not a technology. We can build Web API using different technologies such as Java, .NET etc.
ASP.NET Web API
The ASP.NET Web API is an extensible framework for building HTTP based services that can be accessed in different applications on different platforms such as web, windows, mobile etc. It works more or less the same way as ASP.NET MVC web application except that it sends data as a response instead of html view. It is like a webservice or WCF service but the exception is that it only supports HTTP protocol.
following figure shows a Web API and how web and mobile application communicate with it.
Let’s start creating a new project (WebApiDemoProj ) for Web API, with ASP.NET Core.
Step 1
Go to File => New Porject => Select ASP.NET Core Web Application.
Step 2
Write Project Name: WebApiDemoProj and select a location. Click on Create.
Step 3
From the dropdown, select .NET 5 and select API. Click on Create.
Step 4
After creating the project, it should look like the below image. Build and Run the project by clicking on IIS Express.
Step 5
Run IIS Express
By default, it loads JSON content on the browser.
Step 6
Install and configure Swagger
Install the package “Swashbuckle.AspNetCore”. Right-click on your WebAPi project and click on Manage NuGet Packages.
press to the Browse and paste Swashbuckle.AspNetCore then it shows that it is not installed press this and press to install.
Now we have installed Swashbuckle.AspNetCore
Step 7
Update Startup.cs file to enable Swagger.
Insert this to the ConfigureServices function.
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen();
Insert these lines to Configure function.
// Enable middleware to serve generated Swagger as a JSON endpoint.
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger(c =>
{
c.SerializeAsV2 = true;
});
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty;
});
Step 8
Add a new Controller (UserController.cs)
Right-click on the Controller Folder as shown in the below image to create a new controller file.
- API Controller with read/write actions : 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.
Select API Controller with read/write actions, shown in the following figure:
Give any proper name to the controller.
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.
Step 9
Open file launchSettings.json under Properties and in the section “”profiles”: {“
make it an empty string so that it will load swagger with a list of controllers and corresponding APIs.
By default, launchUrl is set to “api/value”. and shows result inj json format when you run IIS Express
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.
Conclusion
In this post I have described, how to create a basic WEB API in ASP.NET Core via Visual Studio and implemented Swagger with this project which can show a UI to user to interact with backend application (like as DB).
In my Next post, I am going to show, how to integrate an WEB API with a SQL Server databases.
This post was a part of APIs step by step