|
3 | 3 | var builder = WebApplication.CreateBuilder( args ); |
4 | 4 |
|
5 | 5 | // Add services to the container. |
6 | | -builder.Services.AddApiVersioning(); |
| 6 | + |
| 7 | +// enable api versioning and return the headers |
| 8 | +// "api-supported-versions" and "api-deprecated-versions" |
| 9 | +builder.Services.AddApiVersioning( options => options.ReportApiVersions = true ); |
7 | 10 |
|
8 | 11 | var app = builder.Build(); |
9 | | -var versionSet = app.NewApiVersionSet() |
10 | | - .HasApiVersion( 1.0 ) |
11 | | - .HasApiVersion( 2.0 ) |
12 | | - .ReportApiVersions() |
13 | | - .Build(); |
14 | 12 |
|
15 | 13 | // Configure the HTTP request pipeline. |
16 | 14 |
|
|
19 | 17 | "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" |
20 | 18 | }; |
21 | 19 |
|
| 20 | +var forecast = app.MapGroup( "/weatherforecast" ).WithApiVersionSet(); |
| 21 | + |
22 | 22 | // GET /weatherforecast?api-version=1.0 |
23 | | -app.MapGet( "/weatherforecast", () => |
24 | | - { |
25 | | - return Enumerable.Range( 1, 5 ).Select( index => |
26 | | - new WeatherForecast |
27 | | - ( |
28 | | - DateTime.Now.AddDays( index ), |
29 | | - Random.Shared.Next( -20, 55 ), |
30 | | - summaries[Random.Shared.Next( summaries.Length )] |
31 | | - ) ); |
32 | | - } ) |
33 | | - .WithApiVersionSet( versionSet ) |
34 | | - .MapToApiVersion( 1.0 ); |
| 23 | +forecast.MapGet( "/", () => |
| 24 | + { |
| 25 | + return Enumerable.Range( 1, 5 ).Select( index => |
| 26 | + new WeatherForecast |
| 27 | + ( |
| 28 | + DateTime.Now.AddDays( index ), |
| 29 | + Random.Shared.Next( -20, 55 ), |
| 30 | + summaries[Random.Shared.Next( summaries.Length )] |
| 31 | + ) ); |
| 32 | + } ) |
| 33 | + .HasApiVersion( 1.0 ); |
35 | 34 |
|
36 | 35 | // GET /weatherforecast?api-version=2.0 |
37 | | -app.MapGet( "/weatherforecast", () => |
38 | | - { |
39 | | - return Enumerable.Range( 0, summaries.Length ).Select( index => |
40 | | - new WeatherForecast |
41 | | - ( |
42 | | - DateTime.Now.AddDays( index ), |
43 | | - Random.Shared.Next( -20, 55 ), |
44 | | - summaries[Random.Shared.Next( summaries.Length )] |
45 | | - ) ); |
46 | | - } ) |
47 | | - .WithApiVersionSet( versionSet ) |
48 | | - .MapToApiVersion( 2.0 ); |
| 36 | +forecast.MapGet( "/", () => |
| 37 | + { |
| 38 | + return Enumerable.Range( 0, summaries.Length ).Select( index => |
| 39 | + new WeatherForecast |
| 40 | + ( |
| 41 | + DateTime.Now.AddDays( index ), |
| 42 | + Random.Shared.Next( -20, 55 ), |
| 43 | + summaries[Random.Shared.Next( summaries.Length )] |
| 44 | + ) ); |
| 45 | + } ) |
| 46 | + .HasApiVersion( 2.0 ); |
49 | 47 |
|
50 | 48 | // POST /weatherforecast?api-version=2.0 |
51 | | -app.MapPost( "/weatherforecast", ( WeatherForecast forecast ) => { } ) |
52 | | - .WithApiVersionSet( versionSet ) |
53 | | - .MapToApiVersion( 2.0 ); |
| 49 | +forecast.MapPost( "/", ( WeatherForecast forecast ) => { } ) |
| 50 | + .HasApiVersion( 2.0 ); |
54 | 51 |
|
55 | 52 | // DELETE /weatherforecast |
56 | | -app.MapDelete( "/weatherforecast", () => { } ) |
57 | | - .WithApiVersionSet( versionSet ) |
58 | | - .IsApiVersionNeutral(); |
| 53 | +forecast.MapDelete( "/", () => Results.NoContent() ) |
| 54 | + .IsApiVersionNeutral(); |
59 | 55 |
|
60 | 56 | app.Run(); |
61 | 57 |
|
|
0 commit comments