I want SwaggerUI to be enabled during development and staging but disabled in the production environment.
I have follow this code:
appsettings.json "AppSettings": { "SwaggerEnabledSettings": true, }
programme.cs
if (app.Environment.IsDevelopment() || app.Environment.IsProduction())
{
app.UseSwaggerAuthorized();
app.UseDeveloperExceptionPage();
app.UseSwagger();
if (swaggerEnabled)
{
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyAPI V1");
c.RoutePrefix = "";
});
}
}
In above code working fine in local environment I have already configured my application to read the SwaggerEnabledSettings from appsettings.json to determine whether SwaggerUI should be enabled or not. currently, I need to restart the application for changes in this setting to take effect. However, I need assistance in ensuring that this configuration dynamically affects the behavior of SwaggerUI without requiring a restart of the application.
You could use custom middleware to achieve your requirement:
create class AppSettings:
Program.cs:
DynamicMiddleware:
program.cs: