-
Notifications
You must be signed in to change notification settings - Fork 708
/
Copy pathProgram.cs
60 lines (47 loc) · 1.78 KB
/
Program.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var builder = WebApplication.CreateBuilder( args );
// Add services to the container.
// 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.MapApiGroup();
// 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( "/", () =>
{
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();
app.Run();
internal record WeatherForecast( DateTime Date, int TemperatureC, string? Summary )
{
public int TemperatureF => 32 + (int) ( TemperatureC / 0.5556 );
}