Block Query 🚀

How to omit methods from Swagger documentation on WebAPI using Swashbuckle

February 18, 2025

📂 Categories: C#
How to omit methods from Swagger documentation on WebAPI using Swashbuckle

Controlling the visibility of your API endpoints inside your Swagger documentation is important for sustaining a cleanable, centered, and unafraid developer education. Ideate handing a developer a representation with all azygous backmost alley and work entranceway alongside the chief roads – overwhelming, correct? That’s what an unfiltered Swagger papers tin awareness similar. This station volition usher you done respective methods for omitting circumstantial strategies from your Swagger documentation utilizing Swashbuckle for Net API, giving you good-grained power complete what your customers seat. We’ll research property-primarily based hiding, controller-flat customization, and equal planetary filters, making certain your Swagger UI is arsenic streamlined and informative arsenic imaginable.

Hiding Strategies with the [ApiExplorerSettings] Property

1 of the about easy methods to fell an act methodology is by utilizing the [ApiExplorerSettings(IgnoreApi = actual)] property straight supra the technique explanation. This tells Swashbuckle to exclude this circumstantial methodology from the generated Swagger papers. It’s a elemental, but almighty manner to declutter your documentation and fell inner oregon experimental endpoints.

For illustration:

[ApiController] national people MyController : ControllerBase { [HttpGet("national")] national IActionResult PublicMethod() { ... } [ApiExplorerSettings(IgnoreApi = actual)] [HttpGet("backstage")] national IActionResult PrivateMethod() { ... } } 

Successful this illustration, the PublicMethod volition beryllium included successful the Swagger UI, piece PrivateMethod volition beryllium hidden.

Controller-Flat Customization with [ApiExplorerSettings]

For broader power, you tin use the [ApiExplorerSettings] property astatine the controller flat. This permits you to fell each strategies inside a controller, making it perfect for controllers devoted to inner operations oregon inheritance duties. This attack prevents cluttering your API documentation with endpoints not meant for outer depletion.

Planetary Filtering with IDocumentFilter

For precocious situations requiring dynamic power complete visibility, instrumentality the IDocumentFilter interface. This permits you to programmatically manipulate the Swagger papers, giving you the powerfulness to filter strategies primarily based connected analyzable standards, specified arsenic roles, environments, oregon customized attributes. This is peculiarly utile for ample APIs wherever making use of attributes individually mightiness beryllium cumbersome. You tin equal combine with configuration settings to specify visibility guidelines based mostly connected your deployment situation.

Utilizing Swashbuckle’s IOperationFilter

The IOperationFilter presents different almighty attack. By implementing this interface, you tin intercept and modify idiosyncratic cognition descriptions inside the Swagger papers. This granular power permits you to distance circumstantial operations primarily based connected standards similar HTTP methodology, path, oregon equal customized attributes you specify. You tin tailor the visibility of all cognition to lawsuit your circumstantial documentation wants.

Champion Practices for Hiding Strategies

Once deciding which strategies to fell, see the pursuing champion practices:

  • Fell inner oregon administrative endpoints not meant for national depletion.
  • Intelligibly papers immoderate hidden endpoints elsewhere, if wanted, for inner groups.
  • Usage a accordant scheme crossed your API for predictable behaviour.

By pursuing these practices, you guarantee a cleanable and targeted Swagger UI, making it simpler for builders to realize and combine with your API.

Selecting the correct method relies upon connected the complexity of your wants. For elemental hiding, the [ApiExplorerSettings] property is adequate. For much dynamic power, IDocumentFilter oregon IOperationFilter supply higher flexibility. See your task’s necessities and take the attack that champion fits your wants.

[Infographic Placeholder: Illustrating the antithetic strategies of hiding endpoints]

FAQ: Communal Questions Astir Hiding Swagger Endpoints

Q: Tin I fell full controllers?

A: Sure, by making use of the [ApiExplorerSettings(IgnoreApi = actual)] property astatine the controller flat.

Q: What astir hiding primarily based connected person roles?

A: This requires utilizing the IDocumentFilter for much precocious filtering logic.

Managing your Swagger documentation efficaciously is a captious facet of API improvement. By mastering these methods to omit pointless strategies, you empower builders with a broad, concise, and targeted knowing of your API’s capabilities. This not lone improves the developer education however besides enhances safety by minimizing vulnerability of inner endpoints. Retrieve, a fine-structured Swagger papers is a cardinal constituent of a palmy API. This enhances the general developer education and contributes to the wider adoption of your API. Privation to streamline your API documentation additional? Cheque retired our usher connected champion practices for Swagger documentation. Additional speechmaking: Microsoft’s documentation connected Swashbuckle, a adjuvant tutorial connected hiding endpoints, and a deeper dive into cognition filters. Research these assets and elevate your API documentation to the adjacent flat.

  1. Place the strategies you want to omit.
  2. Take the due method (property, controller-flat, oregon filter).
  3. Instrumentality the chosen method and trial the Swagger UI.

Question & Answer :
I person a C# ASP.Nett WebAPI exertion with API documentation being robotically generated utilizing Swashbuckle. I privation to beryllium capable to omit definite strategies from the documentation however I tin’t look to activity retired however to archer Swagger not to see them successful the Swagger UI output.

I awareness it is thing to bash with including a exemplary oregon schema filter however it isn’t apparent what to bash and the documentation lone appears to supply examples of however to modify the output for a methodology, not distance it wholly from the output.

You tin adhd the pursuing property to Controllers and Actions to exclude them from the generated documentation: [ApiExplorerSettings(IgnoreApi = actual)]