Home > Enterprise >  Why aren't Swagger services added to DI container conditionally (only in development)?
Why aren't Swagger services added to DI container conditionally (only in development)?

Time:06-03

The default template of ASP.NET Core Web API is given as follows.

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();

// Why are the following two statements not enabled only in development?
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

// Others are removed for the sake of simplicity.

app.Run();

As you can see, UseSwagger() and UseSwaggerUI() are activated only in development.

Question

I am curious why AddEndpointsApiExplorer() and AddSwaggerGen() are not activated only in development as well.

CodePudding user response:

In enter image description here

  • Related