Swagger and Swashbuckle: Disable Try It Out

In last week’s post, I walked through adding Swagger support to an ASP.NET Core 2 API using the Swashbuckle. One of the awesome things about Swashbuckle is it provides an integration with swagger-ui.

Try it out!

One of the features of the UI integration is the ability to invoke an end point using the “Try it out!” button. This feature is awesome during development but may not be something you want to allow, depending on the use case, for a production end point.

Disable Try it out!

I tried googling lots of different things to find the option to disable the “Try it out” button and had a really hard time finding an answer. It didn’t help that I want the button text to be “Try it now” for some reason. Thankfully it truly was a failure on my part and there is a provided way to disable “Try it out” and it is much more flex able than what I was initially looking for.

In the Configure function of the Startup class find the call to app.UseSwaggerUI. Adding c.SupportedSubmitMethods(new string[] {}); will completely disable “Try it out”. The following is the full call to app.UseSwaggerUI just to provide context.

app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Contacts API V1");
    c.SupportedSubmitMethods(new string[] {});
});

The great thing about the way this is set up if you can allow some actions and not others. For example, say you wanted to allow get actions but disable the rest. The following would allow for that.

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

One word of caution the above is case sensitive and if you use Get instead of get “Try it out” will remain disabled.


Also published on Medium.

12 thoughts on “Swagger and Swashbuckle: Disable Try It Out”

    1. The code should go in the Configure function of the Startup class where you are configuring your SwaggerUi. This is all assuming ASP.NET Core 2.

          1. Thanks for your reply. :)
            I tried to disable” try it out” in .NET Core 1.1 but failed.
            Please let me know if you make it. Thank you very much.

  1. Just a small heads up, the SupportedSubmitMethods parameters now takes a param list of the ‘SubmitMethod’ enum.
    So if you want to specify which methods can be tested based on HTTP verbs your option needs to look like this:

    c.SupportedSubmitMethods(SubmitMethod.Get, SubmitMethod.Head);

  2. Guys, quick question. Can we disable “try it out” from online swagger editor also or is this just for UI only.

    Please if someone can help me with it.

    Thanks

Leave a Reply to Paul M Cancel Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.