Thursday, January 31, 2019

Swagger UI Disable Swagger document

In this blog, we will discuss how to disable the swagger so that user can only view the API document and not able to submit any request through Swagger UI.



Here is swagger document for Order API:




You can see, here Try it Out! Button is available and user can submit the request and get Order Information by order number.

If you are API in Production or any other sensitive environment and don’t want to user to allow to submit any kind of request [GET/POST/Put] but still you want to publish the API documentation.

There is a SupportedSubmitMethods(new string[] {}) method in swagger configuration class, that will be allowed to disabled “Try it Out” button.

C#:

public class SwaggerConfig

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

            GlobalConfiguration.Configuration
                .EnableSwagger(c =>
                    {                      
                        c.SingleApiVersion("v1", "Order API Documentation");                     
                     
                    })
                .EnableSwaggerUi(c =>
                    {                      
                       c.SupportedSubmitMethods(new string[] {  });                      
                    });
        }
    }


after change, there is no ‘Try It Out’ button available but rest of API information is still available.


Other option is also available example you want hide ‘Try It Out” button for all HTTP verbs like  POST, HEAD, PUT but it should be available  for GET only

  c.SupportedSubmitMethods(new string[] { "Get" });

Other swagger related posts

No comments:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...