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.
Other swagger related posts
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" });
No comments:
Post a Comment