Swagger is a great tool for documenting APIs. This article will show you how to set it up with ASP.NET Core.
Getting Started
Swashbuckle.AspNetCore is a nuget package, which includes some Swagger tools for documenting APIs built on ASP.NET Core. Install the latest version to your API project.
Setup the dependency injection:
Use the middlewares:
Swagger UI is now ready to use.
XML Comments
XML Comments are a special kind of comment to document the program. Swagger can be configured to include the documentation.
Make sure the project file is configured to generate the XML file.
Bearer Security Definition
Most APIs use bearer authentication header to protect the endpoints. We can configure Swagger to ask for a bearer authentication header. Refer to this blog.
Unauthorized Response
For protected endpoints, we can use Operation Filter to add a 401 status code.