Introduction
ASP.NET Web API and ASP.NET MVC previously had separate types and setups, but the Web API from ASP.NET Core is the same as the one from ASP.NET Core MVC. The Web API offers a simple communication way based on Representational State Transfer (REST). With REST, HTTP verbs such as GET, POST, PUT, and DELETE is used. GET is used to retrieve resources, POST is used to add new resources, PUT is used to update resources, and DELETE is used to delete resources.
Building Services
Using .NET Core, you need to start with an ASP.NET Core Web Application and select the Web API in the following dialog. This template adds folders and references needed with the Web API. The directory structure that is created with this template contains folders that are needed for creating the services. The Controllers directory contains the Web API controllers, Web API, and ASP.NET Core MVC makes use of the same infrastructure.
![ASP.NET Core 3.1 Web API And Swagger]()
Section 1
Create a PlaceInfo datatable in MSSQL. Also, it creates a class PlaceInfo in the model folder.
PlaceInfo Table Structure
PlaceInfo Class
Create a Services folder in project and add two classes, one is for interface IPlaceInfoService and add method in interface Add(),AddRange(),GetAll(),Find(),Remove() & Update(). Second create PlaceInfoService class is derived from IPlaceInfoService interface. This defines the body of all interface methods, Add(), AddRange(), GetAll(), Find(), Remove() & Update() in PlaceInfoService class.
Created three private functions which are named ExecuteCRUDByQuery,ExecuteQuery and GetPlaceInfoByRow. All three functions are used to communicate with theSQL database server. The first function ExecuteCRUDByQuery is used to insert, update and delete record in datatable by passing SQL query. The second function ExecuteQuery is used to get records from datatable by passing sql query. Function GetPlaceInfoByRow is used to get records from datarow to PlaceInfo model class.
Section 2
Section 2 is the controller code part. Add PlaceInfoController class in Controller folder. Add an attribute on top of class [ApiController], [Route("api/[controller]")]. Also, pass IPlaceInfoService in PlaceInfoController constructors. Create an API method GetPlaceInfos(), GetPlaceInfoById(), PostPlaceInfo(), PutPlaceInfo() & Delete() in PlaceInfoController.The API method returns a response as per the request called by user.
Section 3
Section 3 is the Swagger part. For adding Swagger or OpenAPI to an ASP.NET Web API service, you can use Swashbuckle. The NuGet package Swashbuckle.AspNetCore is the library for ASP.NET Core. After you add the NuGet package, you need to add Swagger to the DI container. AddSwaggerGen is an extension method to add swagger services to the collection. To configure Swagger, you invoke the method SwaggerDoc. Passing an Info object, you can define the title, description, contact information, and more in code file Startup.cs.
![ASP.NET Core 3.1 Web API And Swagger]()
Next open Startup.cs class and edit function ConfigureServices. Call AddSingleton from services object. Then pass interface IPlaceInfoService and class PlaceInfoService in the method. Next, Add Swagger info object to show small documentation for the API.
Next, edit the function Configure and add two method line for Swagger gui interface. These methods are app.UseSwagger() and app.UseSwaggerUi() pass SwaggerEndpoint(service_url,service_name) info.
The extension method UseSwagger adds Swagger middleware and specifies that a JSON schema file should be generated. The default URI that you can configure with UseSwagger is /swagger/{version}/swagger.json. With the document configured in the previous code snippet, the URL is /swagger/v1/swagger.json. The method UseSwaggerUi enables the graphical user interface for Swagger and defines the URL in code file Startup.cs.
Finally, the update key launchUrl in launchsettings.json file. Key-value is "swagger", then build and run the Web API project.
Output
![ASP.NET Core 3.1 Web API And Swagger]()
Conclusion
This article shows and explains to beginners how to create a Web API in ASP.NET Core and use Swagger. Swagger is a GUI interface to communicate with a Web API. Check a Youtube video of the tutorial to understand more about creating a Web API and using Swagger.