Skip to content

Commit 5a3ae7e

Browse files
Refactored Minimal APIs with grouping support
1 parent 3d72e7a commit 5a3ae7e

17 files changed

+1675
-749
lines changed

examples/AspNetCore/WebApi/MinimalApiExample/Program.cs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
var builder = WebApplication.CreateBuilder( args );
44

55
// 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 );
710

811
var app = builder.Build();
9-
var versionSet = app.NewApiVersionSet()
10-
.HasApiVersion( 1.0 )
11-
.HasApiVersion( 2.0 )
12-
.ReportApiVersions()
13-
.Build();
1412

1513
// Configure the HTTP request pipeline.
1614

@@ -19,43 +17,41 @@
1917
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
2018
};
2119

20+
var forecast = app.MapGroup( "/weatherforecast" ).WithApiVersionSet();
21+
2222
// 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 );
3534

3635
// 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 );
4947

5048
// 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 );
5451

5552
// DELETE /weatherforecast
56-
app.MapDelete( "/weatherforecast", () => { } )
57-
.WithApiVersionSet( versionSet )
58-
.IsApiVersionNeutral();
53+
forecast.MapDelete( "/", () => Results.NoContent() )
54+
.IsApiVersionNeutral();
5955

6056
app.Run();
6157

0 commit comments

Comments
 (0)