-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApiVersioningStartup.cs
44 lines (42 loc) · 1.87 KB
/
ApiVersioningStartup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using Microsoft.AspNetCore.Mvc.Versioning;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Reflection;
using static ProtoBuf.Grpc.Configuration.ProtoBufMarshallerFactory;
namespace autogenerate_proto
{
public static class ApiVersioningStartup
{
public static IServiceCollection AddApiVersioningStartup(this IServiceCollection services)
{
// Add Api Versioning
services.AddApiVersioning(opt =>
{
opt.DefaultApiVersion = new Microsoft.AspNetCore.Mvc.ApiVersion(1, 0);
opt.AssumeDefaultVersionWhenUnspecified = true;
opt.ReportApiVersions = true;
opt.ApiVersionReader = ApiVersionReader.Combine(new UrlSegmentApiVersionReader(),
new HeaderApiVersionReader("x-api-version"),
new MediaTypeApiVersionReader("x-api-version"));
/*opt.ApiVersionReader = new UrlSegmentApiVersionReader();*/
});
// Add ApiExplorer to discover versions
services.AddVersionedApiExplorer(o =>
{
o.GroupNameFormat = "'v'VVV";
o.SubstituteApiVersionInUrl = true;
});
services.AddSwaggerGen(o =>
{
o.OperationFilter<SwaggerDefaultValues>();
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
o.IncludeXmlComments(xmlPath);
});
// Configure Swagger
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerGenOptions>();
return services;
}
}
}