Skip to content

Commit ce39eef

Browse files
Modernize example projects for ASP.NET Core 6.0
1 parent bbbf35a commit ce39eef

File tree

28 files changed

+458
-682
lines changed

28 files changed

+458
-682
lines changed
Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
namespace ApiVersioning.Examples;
1+
using Asp.Versioning;
2+
using Microsoft.AspNetCore.OData;
23

3-
using Microsoft.AspNetCore;
4-
using Microsoft.AspNetCore.Builder;
5-
using Microsoft.AspNetCore.Hosting;
4+
var builder = WebApplication.CreateBuilder( args );
65

7-
public static class Program
8-
{
9-
public static void Main( string[] args ) =>
10-
CreateWebHostBuilder( args ).Build().Run();
6+
// Add services to the container.
117

12-
public static IWebHostBuilder CreateWebHostBuilder( string[] args ) =>
13-
WebHost.CreateDefaultBuilder( args )
14-
.UseStartup<Startup>();
15-
}
8+
builder.Services.AddControllers().AddOData();
9+
builder.Services.AddApiVersioning(
10+
options =>
11+
{
12+
// reporting api versions will return the headers
13+
// "api-supported-versions" and "api-deprecated-versions"
14+
options.ReportApiVersions = true;
15+
16+
// allows a client to make a request without specifying an
17+
// api version. the value of options.DefaultApiVersion will
18+
// be 'assumed'; this is meant to grandfather in legacy apis
19+
options.AssumeDefaultVersionWhenUnspecified = true;
20+
21+
// allow multiple locations to request an api version
22+
options.ApiVersionReader = ApiVersionReader.Combine(
23+
new QueryStringApiVersionReader(),
24+
new HeaderApiVersionReader( "api-version", "x-ms-version" ) );
25+
} )
26+
.AddOData( options => options.AddRouteComponents( "api" ) );
27+
28+
var app = builder.Build();
29+
30+
// Configure the HTTP request pipeline.
31+
32+
app.UseHttpsRedirection();
33+
app.UseAuthorization();
34+
app.MapControllers();
35+
app.Run();

examples/AspNetCore/OData/ODataAdvancedExample/Properties/launchSettings.json

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@
33
"windowsAuthentication": false,
44
"anonymousAuthentication": true,
55
"iisExpress": {
6-
"applicationUrl": "http://localhost:1237/",
7-
"sslPort": 0
6+
"applicationUrl": "http://localhost:21237/",
7+
"sslPort": 44328
88
}
99
},
1010
"profiles": {
11-
"IIS Express": {
12-
"commandName": "IISExpress",
11+
"ODataAdvancedExample": {
12+
"commandName": "Project",
13+
"dotnetRunMessages": true,
1314
"launchBrowser": true,
14-
"launchUrl": "api",
15+
"launchUrl": "api/People?api-version=1.0",
16+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
1517
"environmentVariables": {
1618
"ASPNETCORE_ENVIRONMENT": "Development"
1719
}
1820
},
19-
"ODataAdvancedExample": {
20-
"commandName": "Project",
21+
"IIS Express": {
22+
"commandName": "IISExpress",
2123
"launchBrowser": true,
22-
"launchUrl": "http://localhost:5000/api",
24+
"launchUrl": "api/People?api-version=1.0",
2325
"environmentVariables": {
2426
"ASPNETCORE_ENVIRONMENT": "Development"
2527
}

examples/AspNetCore/OData/ODataAdvancedExample/Startup.cs

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1-
namespace ApiVersioning.Examples;
1+
using Microsoft.AspNetCore.OData;
22

3-
using Microsoft.AspNetCore;
4-
using Microsoft.AspNetCore.Builder;
5-
using Microsoft.AspNetCore.Hosting;
3+
var builder = WebApplication.CreateBuilder( args );
64

7-
public static class Program
8-
{
9-
public static void Main( string[] args ) =>
10-
CreateWebHostBuilder( args ).Build().Run();
5+
// Add services to the container.
116

12-
public static IWebHostBuilder CreateWebHostBuilder( string[] args ) =>
13-
WebHost.CreateDefaultBuilder( args )
14-
.UseStartup<Startup>();
15-
}
7+
builder.Services.AddControllers().AddOData();
8+
builder.Services.AddApiVersioning()
9+
.AddOData(
10+
options =>
11+
{
12+
// INFO: you do NOT and should NOT use both the query
13+
// string and url segment methods together. this configuration
14+
// is merely illustrating that they can coexist and allows you
15+
// to easily experiment with either configuration. one of these
16+
// would be removed in a real application.
17+
18+
// WHEN VERSIONING BY: query string, header, or media type
19+
options.AddRouteComponents( "api" );
20+
21+
// WHEN VERSIONING BY: url segment
22+
options.AddRouteComponents( "api/v{version:apiVersion}" );
23+
} );
24+
25+
var app = builder.Build();
26+
27+
// Configure the HTTP request pipeline.
28+
29+
app.UseHttpsRedirection();
30+
app.UseAuthorization();
31+
app.MapControllers();
32+
app.Run();

examples/AspNetCore/OData/ODataBasicExample/Properties/launchSettings.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@
44
"anonymousAuthentication": true,
55
"iisExpress": {
66
"applicationUrl": "http://localhost:1238/",
7-
"sslPort": 0
7+
"sslPort": 44328
88
}
99
},
1010
"profiles": {
11-
"IIS Express": {
12-
"commandName": "IISExpress",
11+
"ODataBasicExample": {
12+
"commandName": "Project",
13+
"dotnetRunMessages": true,
1314
"launchBrowser": true,
1415
"launchUrl": "api/People?api-version=1.0",
16+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
1517
"environmentVariables": {
1618
"ASPNETCORE_ENVIRONMENT": "Development"
1719
}
1820
},
19-
"ODataBasicExample": {
20-
"commandName": "Project",
21+
"IIS Express": {
22+
"commandName": "IISExpress",
2123
"launchBrowser": true,
22-
"launchUrl": "http://localhost:5000/api/$metadata",
24+
"launchUrl": "api/People?api-version=1.0",
2325
"environmentVariables": {
2426
"ASPNETCORE_ENVIRONMENT": "Development"
2527
}

examples/AspNetCore/OData/ODataBasicExample/Startup.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,55 @@
1-
namespace ApiVersioning.Examples;
1+
using ApiVersioning.Examples.Controllers;
2+
using Asp.Versioning.Conventions;
3+
using Microsoft.AspNetCore.OData;
24

3-
using Microsoft.AspNetCore;
4-
using Microsoft.AspNetCore.Builder;
5-
using Microsoft.AspNetCore.Hosting;
5+
var builder = WebApplication.CreateBuilder( args );
66

7-
public static class Program
8-
{
9-
public static void Main( string[] args ) =>
10-
CreateWebHostBuilder( args ).Build().Run();
7+
// Add services to the container.
118

12-
public static IWebHostBuilder CreateWebHostBuilder( string[] args ) =>
13-
WebHost.CreateDefaultBuilder( args )
14-
.UseStartup<Startup>();
15-
}
9+
builder.Services.AddControllers().AddOData();
10+
builder.Services.AddApiVersioning(
11+
options =>
12+
{
13+
// reporting api versions will return the headers
14+
// "api-supported-versions" and "api-deprecated-versions"
15+
options.ReportApiVersions = true;
16+
} )
17+
.AddMvc(
18+
options =>
19+
{
20+
// apply api versions using conventions rather than attributes
21+
options.Conventions.Controller<OrdersController>()
22+
.HasApiVersion( 1.0 );
23+
24+
options.Conventions.Controller<PeopleController>()
25+
.HasApiVersion( 1.0 )
26+
.HasApiVersion( 2.0 )
27+
.Action( c => c.Patch( default, default, default ) ).MapToApiVersion( 2.0 );
28+
29+
options.Conventions.Controller<People2Controller>()
30+
.HasApiVersion( 3.0 );
31+
} )
32+
.AddOData(
33+
options =>
34+
{
35+
// INFO: you do NOT and should NOT use both the query
36+
// string and url segment methods together. this configuration
37+
// is merely illustrating that they can coexist and allows you
38+
// to easily experiment with either configuration. one of these
39+
// would be removed in a real application.
40+
41+
// WHEN VERSIONING BY: query string, header, or media type
42+
options.AddRouteComponents( "api" );
43+
44+
// WHEN VERSIONING BY: url segment
45+
options.AddRouteComponents( "api/v{version:apiVersion}" );
46+
} );
47+
48+
var app = builder.Build();
49+
50+
// Configure the HTTP request pipeline.
51+
52+
app.UseHttpsRedirection();
53+
app.UseAuthorization();
54+
app.MapControllers();
55+
app.Run();

examples/AspNetCore/OData/ODataConventionsExample/Properties/launchSettings.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@
33
"windowsAuthentication": false,
44
"anonymousAuthentication": true,
55
"iisExpress": {
6-
"applicationUrl": "http://localhost:1238/",
7-
"sslPort": 0
6+
"applicationUrl": "http://localhost:21238/",
7+
"sslPort": 44328
88
}
99
},
1010
"profiles": {
11-
"IIS Express": {
12-
"commandName": "IISExpress",
11+
"ODataConventionsExample": {
12+
"commandName": "Project",
13+
"dotnetRunMessages": true,
1314
"launchBrowser": true,
1415
"launchUrl": "api/People?api-version=1.0",
16+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
1517
"environmentVariables": {
1618
"ASPNETCORE_ENVIRONMENT": "Development"
1719
}
1820
},
19-
"ODataConventionsExample": {
20-
"commandName": "Project",
21+
"IIS Express": {
22+
"commandName": "IISExpress",
2123
"launchBrowser": true,
22-
"launchUrl": "http://localhost:5000/api/People?api-version=1.0",
24+
"launchUrl": "api/People?api-version=1.0",
2325
"environmentVariables": {
2426
"ASPNETCORE_ENVIRONMENT": "Development"
2527
}

0 commit comments

Comments
 (0)