-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
66 lines (53 loc) · 3.35 KB
/
Program.cs
File metadata and controls
66 lines (53 loc) · 3.35 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using Asp.Versioning;
var builder = WebApplication.CreateBuilder(args).AddServiceDefaults();
builder.Services.AddControllers();
builder.Services.AddApiVersioning(options =>
{
// Supported/deprecated API versions will be reported in response headers
// This is optional, but can be useful for clients to understand what versions are available
// or for logging and analytics purposes
options.ReportApiVersions = true;
// Set the default API version to 1.0 explicitly
// This is already set to 1.0 by default, but shown here for demonstration
options.DefaultApiVersion = new ApiVersion(1, 0);
// If the user does not specify a version, you can let the API use the default version
// This is disabled by default.
// Enabling this feature is a trade-off between convenience and explicitness.
// Changing the default version could break clients that aren't using versioning.
// Consider your API's audience and usage patterns when deciding to enable this.
options.AssumeDefaultVersionWhenUnspecified = true;
// API versioning by query string (default approach)
// Using query string: ?api-version=1.0
options.ApiVersionReader = new QueryStringApiVersionReader("api-version");
// Alternative: API versioning by HTTP header
// Uncomment the line below to use header-based versioning instead
// options.ApiVersionReader = new HeaderApiVersionReader("x-api-version");
// You can also combine multiple readers
// options.ApiVersionReader = ApiVersionReader.Combine(
// new QueryStringApiVersionReader("api-version"),
// new HeaderApiVersionReader("x-api-version")
// );
})
.AddApiExplorer(options =>
{
// Calling "AddApiExplorer" is required for OpenAPI versioning to work correctly.
// Without this, the generated OpenAPI documents will not be versioned.
// GroupNameFormat specifies the format of the API version.
// Without this, versioning will use the literal group names. In our case, that would be 1.0.
// For compatibility with the "default" /openapi/v1.json behavior from Microsoft.AspNetCore.OpenApi, we use v'VVV' so we can retrieve it using v1.json, v1.0.json and more.
// See https://github.com/dotnet/aspnet-api-versioning/wiki/Version-Format#custom-api-version-format-strings for more information about formatting API versions.
options.GroupNameFormat = "'v'VVV";
})
.AddMvc()
// You must call "AddOpenApi" after "AddApiVersioning" to ensure you use Asp.Versioning's variant.
// This variant of "AddOpenApi" is required to properly integrate with API versioning and generate versioned OpenAPI documents.
// You can call an overload of "AddOpenApi" to customize the OpenAPI generation, just like you would with Microsoft.AspNetCore.OpenApi's "AddOpenApi".
.AddOpenApi();
var app = builder.Build();
// WithDocumentPerVersion() is an extension method provided by the Asp.Versioning.OpenApi package.
// It configures the OpenAPI endpoint to generate a separate document for each API version.
// This allows clients to retrieve documentation specific to the version of the API they are using.
// This approach is preferable compared to having to call "services.AddOpenApi()" multiple times for each version, which can lead to maintenance issues and potential misconfigurations when adding new versions.
app.MapOpenApi().WithDocumentPerVersion();
app.MapControllers();
app.Run();