|
2 | 2 | using Microsoft.AspNetCore.Hosting;
|
3 | 3 | using Microsoft.AspNetCore.HttpOverrides;
|
4 | 4 | using Microsoft.AspNetCore.Mvc;
|
| 5 | +using Microsoft.AspNetCore.Mvc.ApiExplorer; |
5 | 6 | using Microsoft.Extensions.Configuration;
|
6 | 7 | using Microsoft.Extensions.DependencyInjection;
|
7 | 8 | using Newtonsoft.Json.Serialization;
|
@@ -44,17 +45,20 @@ public void ConfigureServices(IServiceCollection services)
|
44 | 45 |
|
45 | 46 | services.AddMvc().AddJsonOptions(options =>
|
46 | 47 | {
|
47 |
| - //Force Camel Case to JSON |
48 | 48 | options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
49 | 49 | }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
|
50 | 50 |
|
| 51 | + services.ConfigureVersionedApiExplorer(); |
| 52 | + |
| 53 | + services.ConfigureApiVersioning(); |
| 54 | + |
51 | 55 | services.ConfigureAutoMapper();
|
52 | 56 |
|
53 |
| - services.ConfigureSwagger(); |
| 57 | + services.ConfigureSwagger(this.GetType()); |
54 | 58 | }
|
55 | 59 |
|
56 | 60 | // 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) |
58 | 62 | {
|
59 | 63 | if (env.IsDevelopment())
|
60 | 64 | {
|
@@ -85,9 +89,13 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
|
85 | 89 |
|
86 | 90 | // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
|
87 | 91 | // specifying the Swagger JSON endpoint.
|
88 |
| - app.UseSwaggerUI(c => |
| 92 | + app.UseSwaggerUI(options => |
89 | 93 | {
|
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 | + } |
91 | 99 | });
|
92 | 100 | }
|
93 | 101 | }
|
|
0 commit comments