Show only specific APIs on Swagger — Asp.Net Core

Alexandre Malavasi
4 min readOct 14, 2020

--

It is pretty common to have Web APIs with tons of Controllers and methods and, sometimes, it is necessary to hide non-public APIs or expose in the Swagger documentation only specific endpoints for any reason. There are a few ways to achieve this object in Asp.Net Core applications. In this article, I explain how to use Document Filters on Swagger in order to control the endpoints that are being shown on the documentation. Given that, let’s do that step by step.

Swagger configuration for Asp.Net Core

First of all, we need to configure the Swagger in the Asp.Net Core project. For demonstration purposes, I created a Asp.Net Core 3.1 Web API project using the default template presented on Visual Studio.

After that, the Swagger packages need to be installed, as seen in the following image:

The Swagger service needs to be specified on the Startup class, as it follows:

From that point, considering I’m using the default template for Web API, it already contains the WeatherForecastController. Apart from the standard get method, I created a second one called PublicGet, as seen in the following image:

If we run the application and type endpoint “swagger” in the URL, we are already able to see the documentation with all the methods in the controller WeatherForecastController:

Now, imagine if we had a requirement specifying that only the PublicGet method needs to be shown on the documentation. We have to create a class implementing the IDocument interface presented in the namespace Swashbuckle.AspNetCore.SwaggerGen, as the representation in the code below:

As you can see, the CustomSwaggerFilter class implements the IDocumentFilter interface, which contains a method called Apply. In this method, I specified a routine to remove from the routes every endpoint that does not have the word “public” as a part of the path. It could have other criteria according to your own scenario: controller name, etc.

In the Startup class a small change is necessary in order to specify the filter when the Swagger service is registered, as seen in the following image:

If we run the application again and call the swagger endpoint, only the PublicGet method is shown in the documentation, as seen in the following image:

It is working as expected. As I said before, there are other ways to apply this filter, but I do like the current approach.

More information

Please, get more information in the documentation below:

https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-3.1&tabs=visual-studio

https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?view=aspnetcore-3.1&viewFallbackFrom=aspnetcore-3.1%3FWT.mc_id%3DDOP-MVP-5003776

Conclusion

Thank you for reading this quick post. If you like the content on the .NET platform feel free to follow me on my social media profiles, including my YouTube channel. Thank you.

Twitter: https://twitter.com/alemalavasi
Linkedin: https://www.linkedin.com/in/alexandremalavasi/
Youtube: https://www.youtube.com/channel/UC-KFGgYiot1eA8QFqIgLmqA
Facebook: https://www.facebook.com/alexandre.malavasi.dev

My Book

I’m glad to announce that I have my first book published. It is a deep dive hands-on through the most common Design Patterns used in .NET applications. The book contains hundreds of code samples and explanations based on real-world scenarios. It also has many examples of Object-Oriented Programming, SOLID principles, and all the path to get yourself familiar with .NET 5 and C#. Check it out:

--

--

Alexandre Malavasi

Microsoft MVP | MCP | MCTS | MCPD | ITIL | .NET | MBA | MTAC | Technical Leader | Consultant | .NET Developer