Vivasoft-logo
[rank_math_breadcrumb]

.NET6 API Documentation & Example request with Swagger

In this post,  Will try to show some basic of Swagger with .NET 6 as well as how to generate example request to make testing handy.

Create a new Web API project with enabling the OpenAPI support.

To add API information and description update builder.Services.AddSwaggerGen() like bellow example:

Run the app and see the API info and description:

To enable xml comment open .csporj file update propertygroup with these two item:

Now configure swagger to use generated xml:

Go to WeatherForecast controller and add comment on Create action like bellow example:

For POST/PUT:

By runnig the app we will see the information are adedd to swagger:

Look into the example value, see that it show the default values. now we want here to add example value for testing. To do so add Swashbuckle.AspNetCore.Filters NuGet package.
update

Now add new class that will generate example value for the WeatherForecast class.

rebuild and run the the app again.

For GET/DELETE request:

Swagger shows all available schema information like above but if we don’t want to show them then just add DefaultModelsExpandDepth(-1)

Implement authorization:

Take advantage of authentication and authorization to secure your Swagger UI in .NET Core 6

Thanks…

By Vivasoft Team
By Vivasoft Team
Hire Exceptional Developers Quickly

Find Your Talent

Get Updated with us Regularly
Share this blog on

Hire a Talented Developer from Vivasoft

Lets discuss about your offshore project. We can Help you by our skillful Global team. You can take a free counciling by taking a schedule

Related Post