Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I using swashbuckle v 5.6.0 for api documentation and ui for test in asp.net web api2. 
I do not want give ability for the user to post data from swagger ui in production.
How do I disable it?


What I have tried:

GlobalConfiguration.Configuration
               .EnableSwagger(c =>
                   {
                   c.Schemes(new[] { "http", "https" });
                   c.SingleApiVersion("v1", ""Demo API");
                   c.PrettyPrint();
                   c.IncludeXmlComments(GetXmlCommentsPath());
                       })
              .EnableSwaggerUi(c =>
                   {
                     c.DisableValidator();
                      });
Posted
Updated 19-Jun-22 5:45am
v2

Have you looked into there GIT repository and read this issue?
Disabling swagger ui · Issue #301 · domaindrivendev/Swashbuckle · GitHub[^]
 
Share this answer
 
If you are using NET framework (any version capable of having swagger)

in you web.config,

1. create a new key called IsSwaggerEnabled and set it to true

  <appSettings>
    <add key="IsSwaggerEnabled" value="true" />
</appSettings>


2. If you installed swagger from nuget package already, then you have the (*\App_Start\SwaggerConfig.cs)

in there find the Register() method and add code as the following

public static void Register()
{
         var thisAssembly = typeof(SwaggerConfig).Assembly;

         // if key in config does not exist we set it to true
         // you can play to make it always false ***
         bool isSwaggerEnabled = ConfigurationManager.AppSettings["IsSwaggerEnabled"] != null ?
             bool.Parse(ConfigurationManager.AppSettings["IsSwaggerEnabled"]) : true;

         // if swagger is not enabled we don't even bother to config it,
         // therefor will be disabled, and you don't have to worry about it.
         if (!isSwaggerEnabled)
             return;

         GlobalConfiguration.Configuration
             .EnableSwagger(c =>

 // code below removed for readability
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900