-
Notifications
You must be signed in to change notification settings - Fork 717
Expand file tree
/
Copy pathProgram.cs
More file actions
96 lines (77 loc) · 3.36 KB
/
Program.cs
File metadata and controls
96 lines (77 loc) · 3.36 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using Asp.Versioning;
var builder = WebApplication.CreateBuilder( args );
// Add services to the container.
builder.Services.AddProblemDetails();
// enable api versioning and return the headers
// "api-supported-versions" and "api-deprecated-versions"
builder.Services.AddApiVersioning( options => options.ReportApiVersions = true );
var app = builder.Build();
// Configure the HTTP request pipeline.
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
var forecast = app.NewVersionedApi();
// GET /weatherforecast?api-version=1.0
forecast.MapGet( "/weatherforecast", () =>
{
return Enumerable.Range( 1, 5 ).Select( index =>
new WeatherForecast
(
DateTime.Now.AddDays( index ),
Random.Shared.Next( -20, 55 ),
summaries[Random.Shared.Next( summaries.Length )]
) );
} )
.HasApiVersion( 1.0 );
// GET /weatherforecast?api-version=2.0
var v2 = forecast.MapGroup( "/weatherforecast" )
.HasApiVersion( 2.0 );
v2.MapGet( "/", ( ApiVersion version ) =>
{
return Enumerable.Range( 0, summaries.Length ).Select( index =>
new WeatherForecast
(
DateTime.Now.AddDays( index ),
Random.Shared.Next( -20, 55 ),
summaries[Random.Shared.Next( summaries.Length )]
) );
} );
// POST /weatherforecast?api-version=2.0
v2.MapPost( "/", ( WeatherForecast forecast ) => Results.Ok() );
// DELETE /weatherforecast
forecast.MapDelete( "/weatherforecast", () => Results.NoContent() )
.IsApiVersionNeutral();
// ---- IntroducedInApiVersion demonstration ----
//
// An explicit api version set declares v1.0, v2.0, and v3.0. Endpoints
// attached to it inherit that set, so IntroducedInApiVersion's
// "from-this-version-onward" expansion has the full controller-declared
// set to filter against.
var multiVersionSet = app.NewApiVersionSet( "MultiVersioned" )
.HasApiVersion( new ApiVersion( 1.0 ) )
.HasApiVersion( new ApiVersion( 2.0 ) )
.HasApiVersion( new ApiVersion( 3.0 ) )
.Build();
// .HasApiVersion( 2.0 ) on an endpoint attached to a version set that
// also declares v1.0 and v3.0 is exact-match — equivalent to
// [MapToApiVersion(2.0)]. v1.0 and v3.0 callers receive the configured
// UnsupportedApiVersionStatusCode (default 400).
app.MapGet( "/multiversioned/legacy", ( ApiVersion version ) =>
Results.Ok( $"Legacy {version}" ) )
.WithApiVersionSet( multiVersionSet )
.HasApiVersion( 2.0 );
// .IntroducedInApiVersion( 2.0 ) is "from this version onward against
// the declared set." Reachable for v2.0 AND v3.0 automatically.
// Requests under v1.0 receive the per-attribute status (default 404).
// When v4.0 is added to multiVersionSet, this endpoint becomes reachable
// for v4.0 with no further changes.
app.MapGet( "/multiversioned/modern", ( ApiVersion version ) =>
Results.Ok( $"Modern {version}" ) )
.WithApiVersionSet( multiVersionSet )
.IntroducedInApiVersion( 2.0 );
app.Run();
internal record WeatherForecast( DateTime Date, int TemperatureC, string? Summary )
{
public int TemperatureF => 32 + (int) ( TemperatureC / 0.5556 );
}