Is it possible to disable the UI through some configuration setting? I was looking for an easy way to disable this in our production environment since we don't need to expose this to the outside world. Not a big deal but would be a nice feature to have.
If you look in SwaggerConfig.cs you'll see an explicit call to "EnableSwaggerUi". Just remove that call
Thank you. That worked great
Having multiple builds is a bit irritating. Is it NOT possible then to have a config entry to disable Swagger rather than the re-compile? We use Swagger for our development environment. However, in production we don't want it exposed. For this reason, we'd like to turn it off. However, in the event of debugging, it is extremely useful to be able to enable it again for the duration of the session.
Either way, awesome product!
If you're using .Net CORE, this would allow you to toggle Swagger (this answer specifically refers to the environment variable, but it could also be located in the appsettings.json or appsettings.{ENV}.json)
http://stackoverflow.com/a/32556303/3246805
Essentially...
bool isDev;
public Startup(IHostingEnvironment env) {
//snip...
isDev = env.IsDevelopment();
}
public void ConfigureServices(IServiceCollection services) {
//snip...
if (isDev) services.AddSwaggerGen();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
//snip...
if (isDev) {
// Enable middleware to serve generated Swagger as a JSON endpoint
app.UseSwagger();
// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
app.UseSwaggerUi();
}
}
...or various similar incantations.
Would be really helpful to have a config setting for this.
@togglebrain - You can wire it up yourself (large unrelated snippets omitted)...
appsettings.json
{
"AppSettings": {
"EnableSwagger": true
},
"ConnectionStrings": {
"DbContext": ""
}
}
MyOptions.cs
public class MyOptions {
public bool EnableSwagger = false;
// snip...
}
Startup.cs
public class Startup {
public IConfigurationRoot Configuration { get; }
private MyOptions myOpts { get; }
public Startup(IHostingEnvironment env) {
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
// myopts/class not necessary, just how I implemented it
myOpts = new MyOptions();
var appSettings = Configuration.GetSection("AppSettings");
myOpts.EnableSwagger = bool.Parse(
appSettings.GetValue<string>("EnableSwagger") ?? "true"
);
}
public void ConfigureServices(IServiceCollection services) {
services.AddSingleton<MyOptions>(myOpts);
if (myOpts.EnableSwagger)
services.AddSwaggerGen();
}
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory,
AlexaDbContext db
) {
app.UseMvc();
if (myOpts.EnableSwagger) {
// Enable middleware to serve generated Swagger as a JSON endpoint
app.UseSwagger();
// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
app.UseSwaggerUi();
}
}
}
Thanks @bigteejay but a bit of work asking all existing microservices teams to add this code!! will do though.
Our application is Microsoft stack. We deploy our application in the client environment. We want to turn off the swagger when deploying on the clients infrastructure. If we keep config change in the app.config or database, the end client can easily turn on the swagger and view all our private apis. They also have admin access to the DB. Is there a secured way of turning off the swagger for the on-prem deployment?
Our application is Microsoft stack. We deploy our application in the client environment. We want to turn off the swagger when deploying on the clients infrastructure. If we keep config change in the app.config or database, the end client can easily turn on the swagger and view all our private apis. They also have admin access to the DB. Is there a secured way of turning off the swagger for the on-prem deployment?
You could use preprocessor directives to achieve this.
i.e. in Startup.ConfigureServices:
#if DEBUG
services.AddSwaggerGen(...)
#endif
Additionally you need to surround the methods UseSwagger and UseSwaggerUI in Startup.Configure with this preprocessor directive.
This ensures Swagger is not used in release config.
Most helpful comment
If you're using .Net CORE, this would allow you to toggle Swagger (this answer specifically refers to the environment variable, but it could also be located in the
appsettings.jsonorappsettings.{ENV}.json)http://stackoverflow.com/a/32556303/3246805
Essentially...
...or various similar incantations.