Documenting your controllers
Documenting your API controllers using Swagger in Umbraco Version 14 simplifies the creation of detailed and interactive API documentation. Adding Swagger attributes automatically generates comprehensive information about routes, parameters, and response types. This will enhance the developer experience and ensure clarity and consistency in your API documentation.
ApiExplorerSettings
With the ApiExplorerSettings
attribute, we can put all our endpoints into a given group. This is a nice way of organizing our endpoints in the Swagger UI.
ProducesResponseType Attribute
Use [ProducesResponseType] to specify the possible responses for each action method. This helps Swagger generate accurate documentation for your API. For example, in the GetItem method:
Here, [ProducesResponseType]
specifies that a 200 OK response will return a MyItem, and a 404 Not Found response will return a ProblemDetails.
Example Documentation for Each Controller Method
To get an idea of how to document each controller method, below are some examples of how to document each operation for an API controller. The controller is from the Creating your own API article
GetAllItems
GetItem
CreateItem
UpdateItem
DeleteItem
Verifying the changes
Run your application and navigate to the Swagger UI (typically found at /swagger). Verify that your API documentation is correctly displaying the routes, parameters, and response types.
Last updated