Last week .NET Core was released, and the first thing I tried to solve was how to Add Swagger to your .NET Core Web API:
Dependencies
At the time of writing you should add the following dependency to your project.json file
1 "Swashbuckle": "6.0.0-beta901"
Using statement
In your Startup class add the following using statement
1 using Swashbuckle.Swagger.Model;
Add Swagger as a service
In your Startup class add the following code to the ConfigureServices method
1 // This method gets called by the runtime. Use this method to add services to the container.
2 public void ConfigureServices(IServiceCollection services)
3 {
4 ...
5
6 services.AddSwaggerGen();
7 services.ConfigureSwaggerGen(options =>
8 {
9 options.SingleApiVersion(new Info
10 {
11 Version = "v1",
12 Title = "Your API title here",
13 Description = "Your API description here",
14 TermsOfService = "Your API terms of service here"
15 });
16 });
17 }
Add Swagger to the HTTP request pipeline
And finally add the following lines to the Configure method of your Startup class
1 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
2 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
3 {
4 ...
5
6 app.UseSwagger();
7 app.UseSwaggerUi();
8 }
And that’s it you are ready to go!
Hope it helps!
Comments