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.
I’m trying to implement the same but no idea where to place the code. Would you please elaborate little more.
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.
Could ASP.NET core 1.1 disable ‘try it out’ button?
I would think so but have not tried it in 1.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.
You can just type c.SupportedSubmitMethods(); to remove Try it Out Options.
Diamond! Thank you for this ?
Your welcome.
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);
Thank you for the update Paul!
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
Using .net core 3.1. I can remove the “Try It Out” but that also removes the Execute button..