Skip to content

Commit a829306

Browse files
committed
Update Startup.cs
swagger to display api version automatically
1 parent 50f2853 commit a829306

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

Supermarket/Startup.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.AspNetCore.Hosting;
33
using Microsoft.AspNetCore.HttpOverrides;
44
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.AspNetCore.Mvc.ApiExplorer;
56
using Microsoft.Extensions.Configuration;
67
using Microsoft.Extensions.DependencyInjection;
78
using Newtonsoft.Json.Serialization;
@@ -44,17 +45,20 @@ public void ConfigureServices(IServiceCollection services)
4445

4546
services.AddMvc().AddJsonOptions(options =>
4647
{
47-
//Force Camel Case to JSON
4848
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
4949
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
5050

51+
services.ConfigureVersionedApiExplorer();
52+
53+
services.ConfigureApiVersioning();
54+
5155
services.ConfigureAutoMapper();
5256

53-
services.ConfigureSwagger();
57+
services.ConfigureSwagger(this.GetType());
5458
}
5559

5660
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
57-
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
61+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApiVersionDescriptionProvider provider)
5862
{
5963
if (env.IsDevelopment())
6064
{
@@ -85,9 +89,13 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
8589

8690
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
8791
// specifying the Swagger JSON endpoint.
88-
app.UseSwaggerUI(c =>
92+
app.UseSwaggerUI(options =>
8993
{
90-
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Supermarket API V1");
94+
//Build a swagger endpoint for each discovered API version
95+
foreach (var description in provider.ApiVersionDescriptions)
96+
{
97+
options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
98+
}
9199
});
92100
}
93101
}

0 commit comments

Comments
 (0)