0

I Recently Start working with Asp.net Web API and in the Present of Methods in Program.cs.

What does AddSwaggerGen() do?

var builder = WebApplication.CreateBuilder(args);


builder.Services.AddSwaggerGen();

4 Answers4

2

That line of code, adds the Swagger generator to the services collection. SwaggerUI shows the REST APIs in your project in a user-friendly UI, so that you can test them in the browser. For this UI to render, you need the JSON file that expresses the details of the APIs[name, input arguments, types, etc], that JSON is being generated by this Swagger Generator.

Note: Swagger was the name of the project that described the definition of REST APIs, since 2015 it has been known as OpenAPI.

Anand Sowmithiran
  • 2,591
  • 2
  • 10
  • 22
0

The configuration action passed to the AddSwaggerGen method adds information such as the author, license, and description. You can find more information here.

Ivan Gechev
  • 706
  • 3
  • 9
  • 21
0

AddSwaggerGen() method adds a service that generates Swagger documents for your APIs. When this method is called, it adds the service to the dependency injection container so that it can be used later in the application. When the service is used, it will generate a Swagger document that describes the available APIs and their operations. This can be useful for developers who are consuming the APIs, as it provides detailed information about how to use the APIs.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
0

It is Part of the Swashbuckle Library, Which Provides Support For Generating

OpenAPI

and Swagger documentation for your API

Sample of Use it :

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("Your Version", new OpenApiInfo
    {
        Version = "Your Version",
        Title = "Your Title Api",
        Description = "Desc",
        TermsOfService = new Uri("https://example.com/terms"),
        Contact = new OpenApiContact
        {
            Name = "Shayne Boyer",
            Email = string.Empty,
            Url = new Uri("https://socialNet.com/<user>"),
        },
        License = new OpenApiLicense
        {
            Name = "Use under LICX",
            Url = new Uri("https://example.com/license"),
        }
    });
});