Using Swagger
In this recipe, you will learn how to use Swagger to create help pages and documentation for our REST APIs.
Getting ready
Let's create an empty project with VS 2017.
How to do it...
- First, let's add the
Swashbuckle
reference to the project:
"Swashbuckle": "5.6.0"
- Next, let's add Swagger as a middleware in
Startup.cs
:
public void ConfigureServices(IServiceCollection services) { services.AddMVC(); services.AddSwaggerGen(); } public void Configure(IApplicationBuilder app) { app.UseMVC(); app.UseSwagger(); app.UseSwaggerUi(); }
- Now, let's launch our API documentation by going to http://{UrlAPI}/swagger/ui. We can now see the generated API documentation:
- When we click on each HTTP method, we can see all the options Swagger offers to us, such as testing the API, easily:
- Next, let's use another feature: adding information about the API. To do this, let's add this code to
Startup.cs
:
public class Startup { public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder...