添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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.

I’m trying to implement the same but no idea where to place the code. Would you please elaborate little more.

Reply

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.

Reply

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);

Reply

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

Reply

Using .net core 3.1. I can remove the “Try It Out” but that also removes the Execute button..

Reply

Leave a Comment Cancel Reply

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