.NET Core API with Swagger implementation

.NET Core API With Swagger Implementation

Implementing REST APIs allows developers to solve problems in development such as business logic, user interface, processing, and so on. Generally, REST APIs come in two forms, Stateless APIs and Stateful APIs.

The difference between both API implementations is about the client information stored in a session. While Stateless API does not store the client state session, Stateful stores the same and utilizes it to provide the required response.

Swagger is a suite of API developer tools used to design, document, test, and deploy the API lifecycle. Developers use Swagger to create, build, and modify open-source and commercially available components while building impressive API systems.

This blog is a guide on Swagger implementation with .NET CORE.

Steps for Swagger Implementation

1. Create.NETCore Web API Project

For best results, we are using VS 2019 and .NETCore 3.1 framework.

2. Add a new controller with the name ‘Values Controller’(API Controller -Empty)

3. Create Model Class: Values Model

If you would notice the images above, they represent model class examples with three properties.

4. Create Controller Action Method

When it comes to documentation, action methods are most important. When you create a Controller Action Method, it will create the required UI documentation based on the HTTP Verbs and Attributes.

5. Add Swagger

To add Swagger to our application, we need to install the Swashbuckle.AspNetCore package from Nuget package manager.

Moving forward, you need to add the code script given below in Startup.cs file for API versioning and Swagger API endpoint configuration.

Next, you need to alter the launch URL in launchSettings.json (“swagger/index.html” is the default URL for swagger-UI documentation)

Run the application to view the basic Swagger documentation.

Conclusion

Using Swagger for API implementation is beneficial for a developer and the project they are working on. It provides an easily tested API and allows simulating the usage of any development method. Swaggers are helpful tools for API documentation as well.

To sum it up, Swagger helps developers (humans) and machines to understand the capabilities of REST API without actually depending on interpreting the source code. Using REST APIs reduces the amount of work required to align and document decoupled services.