Is it possible to hide all endpoints by default in swagger, and to show only ones I mark with some kind of attribute?
-
Possible duplicate of [How to omit methods from Swagger documentation on WebAPI using Swashbuckle](https://stackoverflow.com/questions/29701573/how-to-omit-methods-from-swagger-documentation-on-webapi-using-swashbuckle), [Restrict access to certain API controllers in Swagger using Swashbuckle and ASP.NET Identity](https://stackoverflow.com/q/33176319/113116), [Remove a route with IOperationFilter in SwashBuckle](https://stackoverflow.com/q/36190624/113116) – Helen Aug 08 '19 at 12:50
2 Answers
You can use the attribute:
[ApiExplorerSettings(IgnoreApi = true)]
On your custom base controller or just the controllers you want to hide. If you are inheriting from ApiController then you can either make your own base controller or unfortunately mark each controller with it. No need to do individual actions as that will listen to the controller's attribute. If you mark your base controller then you'll need to mark the actions or controllers manually with:
[ApiExplorerSettings(IgnoreApi = false)]
If you want to hide the endpoints because they are not being used anymore but still want the code to be there then you can do two things.
Change the access modifier of those methods to private
Tag all the items you want to hide with the
[Obsolete]
attribute and change the swagger config in startup like this:httpConfiguration.EnableSwagger(c => { c.IgnoreObsoleteActions(); });
-
is there a way to reverse this logic whithout marking all actions and controllers. To clear all paths, excepts ones marked with an attributte? – Robert Aug 08 '19 at 14:02
-
@Robert if you are inheriting from your own base controller then yes you will only have to mark that single controller then everything else will be hidden. If you are inheriting from ApiController then you can either make your own base controller or unfortunately mark each controller with it. No need to do individual actions as that will listen to the controller's attribute – Tom Dee Aug 08 '19 at 14:11
You can add following attribute to your controller to ignore all the endpoints.
[ApiExplorerSettings(IgnoreApi = true)]
You can set IgnoreApi = false for those endpoints which you want to show.
On the other hand, if you apply IgnoreApi = false for a particular method, only that method will get ignored.

- 410
- 4
- 9