How to integrate Swagger into ASP.NET CORE Web API in .Net 6 or .Net 7

This article will explain how to integrate swagger into Asp.Net Core Web API in .Net 6 or .Net 7. An open-source standard for API documentation is called Swagger. Swagger offers an interface for interacting with an API through a browser and enables us to produce documentation from our code. A well-liked tool for developers to document and test APIs is called Swagger. It has numerous characteristics that make it valuable for developers and makes it simple to construct and consume APIs. Because of how simple it makes it to construct and use APIs, it is well-liked in the ASP.Net community. A language-neutral specification called Swagger is used to describe REST APIs. OpenAPI is another name for Swagger. It enables us to comprehend API capabilities without having to look at the implementation code itself. The work required to integrate an API was previously minimized by Swagger. In a similar vein, it supports API developers in efficiently and swiftly documenting their APIs. In this article, we will discuss the steps to integrate Swagger into an ASP.NET Core web API in .NET 6 or .NET 7.


Why should use Swagger:


Swagger is an open-source framework that enables developers to create, document, and test APIs in a simple and user-friendly way. It provides a powerful platform for designing, building, and documenting RESTful APIs. With the release of .NET 6 and .NET 7, developers can integrate Swagger into ASP.NET Core web API applications easily. Because it is simple to use and has many capabilities, Swagger is a popular choice for creating APIs. Swagger is simple to use and offers a wealth of developer-friendly capabilities.

Swagger has several advantages, including the ability to automatically create documentation for your API, assist with API testing, and increase the accessibility of your API to third-party developers. Moreover, Swagger is simple to connect with already-in-use development frameworks and tools.

Some of the features of Swagger at a glance include:

  • The ability to generate documentation for an API


  • The ability to test an API


  • The ability to generate client code for an API



Set up Swagger in .Net6 or .Net 7:


Prerequisites:

Step 1: Create a new ASP.NET Core web API project


First, let's create a new ASP.NET Core web API project in Visual Studio. Open Visual Studio and select "Create a new project." In the new project dialog, select "ASP.NET Core Web API" as the project template and provide a name and location for your project. Click on the "Create" button to create a new project.


Step 2: Install the Swashbuckle.AspNetCore NuGet package


Swashbuckle.AspNetCore is a NuGet package that provides Swagger integration for ASP.NET Core web APIs. To install the package, right-click on the project in the Solution Explorer and select "Manage NuGet Packages." In the "NuGet Package Manager" window, search for "Swashbuckle.AspNetCore" and select the latest version. Then Click on the "Install" button to install the package.


Note: Note that you would need to install the Swagger NuGet package only if you have not selected OpenAPI support when creating the project. If you have already selected OpenAPI support when creating your new project, you can skip this step.


"Swashbuckle.AspNetCore" has a dependency on the following packages which are also automatically installed.

swashbuckle.png

Swashbuckle.AspNetCore.SwaggerGen:
This package produces the SwaggerDocument objects after inspecting our API routes, controllers, and models.

Swashbuckle.AspNetCore.Swagger: SwaggerDocument objects are made available as JSON endpoints by this package.

Swashbuckle.AspNetCore.SwaggerUI: This package analyses SwaggerDocument objects, provides interactive API documentation, and enables in-browser API call testing for your users.


checkopenapi.png


Swagger can be installed via NuGet by using the following command at the NuGet Package Manager Console:


Install-Package Swashbuckle.AspNetCore




Once you have installed this package, the Program.cs file will be updated. Here is how it would look in code:


using Di_Demo.Common.Repository.Interface;

using Di_Demo.Common.Repository;

using Di_Demo.Services;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle

builder.Services.AddEndpointsApiExplorer();

builder.Services.AddSwaggerGen();

                                 

var app = builder.Build();


// Configure the HTTP request pipeline.

if (app.Environment.IsDevelopment())

{

    app.UseSwagger();

    app.UseSwaggerUI();

}


app.UseHttpsRedirection();


var summaries = new[]

{

    "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"

};


app.MapGet("/weatherforecast", () =>

{

    var forecast = Enumerable.Range(1, 5).Select(index =>

        new WeatherForecast

        (

            DateTime.Now.AddDays(index),

            Random.Shared.Next(-20, 55),

            summaries[Random.Shared.Next(summaries.Length)]

        ))

        .ToArray();

    return forecast;

})

.WithName("GetWeatherForecast");


app.Run();


internal record WeatherForecast(DateTime Date, int TemperatureC, string? Summary)

{

    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

}





The Swagger User Interface:


When you execute the application, here is how the Swagger UI will look:

swagger.png


When developers execute the HttpGet action method in Swagger, here is how the output will appear:

one.png

two.png



Conclusion:


In this article, we discussed the steps to integrate Swagger into an ASP.NET Core web API in .NET 6 or .NET 7. We installed the Swashbuckle.AspNetCore NuGet package configured Swagger in our ASP.NET Core web API and tested the Swagger integration by navigating to the Swagger UI URL in the browser. With Swagger, we can easily create, document, and test our API. Swagger is a fantastic tool for API documentation. It also offers an interface for using the action method executions. Swagger can be modified to meet your needs. For instance, you may adjust how Swagger tests your API and add more information to the documentation for your API.

Hope you enjoyed reading this article and found it useful. Please share your thoughts and recommendations in the comment section below.


Share This Post

Linkedin
Fb Share
Twitter Share
Reddit Share

Support Me

Buy Me A Coffee