Integrating Swagger With Web API .NET Core Using Entity Framework

Background

Swagger is an open-source tool that helps to build documentation, test APIs, and generate client code in many languages. Swagger is a set of rules, specifications, and tools that help us document our APIs. In this manner, we can create documentation that is useful for people who need it. Swagger helps us create documentation that everyone can understand.

Swagger UI is an attractive tool that will easily test APIs and save time building the JSON request for GET, POST, PUT and DELETE operations. Each operation will be fulfilled with a list of parameters with their corresponding examples.

If you are new to ASP.NET Core with Web API, I would encourage you to go through the article link - Building ASP.NET Web API in .NET Core using Entity Framework, there I have demonstrated how to build a simple ASP.NET Web API using Entity Framework library. This article is a continuation to integrate a Swagger library to the Web API .Net Core project.

Integrating Swagger library

Step 1

Refer to the Article Building ASP.NET Web API in .NET Core with Entity Framework to build the simple ASP.NET Web API with Entity Framework library. From here onwards, we will continue to integrate the Swagger library.

Step 2

Open the Visual Studio Solution – WebApiCoreWithEF.

Step 3

Install the following Nuget package from Nuget Package Manager.

Integrating Swagger With Web API .NET Core Using Entity Framework

Step 4

Let’s register the swagger doc generator and use Swagger UI as middleware in startup.cs as below,

Integrating Swagger With Web API .NET Core Using Entity Framework

Step 5

Update the launch URL with swagger name in “launchsettings.json”,

Integrating Swagger With Web API .NET Core Using Entity Framework

Step 6

Rebuild the solution and hit on Run button. Now we can see the Swagger UI in the browser as below,

Integrating Swagger With Web API .NET Core Using Entity Framework

Web API Documenting

Step 1

Click on the link - /swagger/v1/swagger.json, you should able to see the JSON representation of Web API definition and schema. Below JSON highlights each category of each operation and gives a meaningful Web API document. This Web API document is very helpful to generate the client code to consume the Web API.

Integrating Swagger With Web API .NET Core Using Entity Framework

Web API Testing

Step 1

Here Swagger provides a wonderful UI to test each Web API with a list of parameters with their corresponding operations. Let’s play around with all the operations.

Step 2

Click on ‘Try it out’ button at the top right corner and it will be displayed with the execution screen.

Step 3 - GET all employee records

It’s very simple, just click on the  ‘Execute’ button and get the all-employees records in response.

Integrating Swagger With Web API .NET Core Using Entity Framework

Step 4 - POST an employee record

Request will give the ready-made template of Employee JSON, we just need to set the values for each field and click on ‘Execute’ button. The record will be created.

Integrating Swagger With Web API .NET Core Using Entity Framework

Step 5 - GET with Employee ID = 2

Here parameter ‘id’ is mandatory and needs to set a value to retrieve the appropriate employee record.

Integrating Swagger With Web API .NET Core Using Entity Framework

Step 6 - PUT with employee records correction

Here parameter ‘id’ is mandatory and needs to set a value to update the appropriate employee record. Here the records are updated with the current and permanent address.

Integrating Swagger With Web API .NET Core Using Entity Framework

Step 7 - DELETE an employee record

Here parameter ‘id’ is mandatory and needs to set a value to delete the appropriate employee record.

Integrating Swagger With Web API .NET Core Using Entity Framework

Here we can see a lot of rich features to test the Web APIs with defining the template of an entity in JSON format and we just need to set the appropriate value for each field in JSON and we're ready to test in a single click.

Summary

We have explored how to Intergate the Swagger library and generate the Web API document. Then, we have used the Swagger UI to test all the Web API quickly and systematically.

I hope this article helps you to understand with the pictorial representation. Thanks for reading the article.


Similar Articles