Skip to content

Commit 9fc044c

Browse files
Chris Martinezcommonsensesoftware
authored andcommitted
Update samples for Endpoint Routing
1 parent 97b7569 commit 9fc044c

File tree

12 files changed

+60
-18
lines changed

12 files changed

+60
-18
lines changed

samples/aspnetcore/BasicSample/Startup.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.AspNetCore.Hosting;
55
using Microsoft.Extensions.Configuration;
66
using Microsoft.Extensions.DependencyInjection;
7+
using static Microsoft.AspNetCore.Mvc.CompatibilityVersion;
78

89
public class Startup
910
{
@@ -17,10 +18,15 @@ public Startup( IConfiguration configuration )
1718
// This method gets called by the runtime. Use this method to add services to the container.
1819
public void ConfigureServices( IServiceCollection services )
1920
{
20-
services.AddMvc();
21-
22-
// reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
23-
services.AddApiVersioning( o => o.ReportApiVersions = true );
21+
// the sample application always uses the latest version, but you may want an explict version such as Version_2_2
22+
// note: Endpoint Routing is enabled by default; however, if you need legacy style routing via IRouter, change it to false
23+
services.AddMvc( options => options.EnableEndpointRouting = true ).SetCompatibilityVersion( Latest );
24+
services.AddApiVersioning(
25+
options =>
26+
{
27+
// reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
28+
options.ReportApiVersions = true;
29+
} );
2430
}
2531

2632
public void Configure( IApplicationBuilder app, IHostingEnvironment env )

samples/aspnetcore/ByNamespaceSample/Startup.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.AspNetCore.Mvc.Versioning.Conventions;
66
using Microsoft.Extensions.Configuration;
77
using Microsoft.Extensions.DependencyInjection;
8+
using static Microsoft.AspNetCore.Mvc.CompatibilityVersion;
89

910
public class Startup
1011
{
@@ -18,7 +19,9 @@ public Startup( IConfiguration configuration )
1819
// This method gets called by the runtime. Use this method to add services to the container.
1920
public void ConfigureServices( IServiceCollection services )
2021
{
21-
services.AddMvc();
22+
// the sample application always uses the latest version, but you may want an explict version such as Version_2_2
23+
// note: Endpoint Routing is enabled by default; however, if you need legacy style routing via IRouter, change it to false
24+
services.AddMvc( options => options.EnableEndpointRouting = true ).SetCompatibilityVersion( Latest );
2225
services.AddApiVersioning(
2326
options =>
2427
{

samples/aspnetcore/ConventionsSample/Startup.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.AspNetCore.Mvc.Versioning.Conventions;
88
using Microsoft.Extensions.Configuration;
99
using Microsoft.Extensions.DependencyInjection;
10+
using static Microsoft.AspNetCore.Mvc.CompatibilityVersion;
1011

1112
public class Startup
1213
{
@@ -20,7 +21,9 @@ public Startup( IConfiguration configuration )
2021
// This method gets called by the runtime. Use this method to add services to the container.
2122
public void ConfigureServices( IServiceCollection services )
2223
{
23-
services.AddMvc();
24+
// the sample application always uses the latest version, but you may want an explict version such as Version_2_2
25+
// note: Endpoint Routing is enabled by default; however, if you need legacy style routing via IRouter, change it to false
26+
services.AddMvc( options => options.EnableEndpointRouting = true ).SetCompatibilityVersion( Latest );
2427
services.AddApiVersioning(
2528
options =>
2629
{

samples/aspnetcore/ODataBasicSample/Startup.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Hosting;
77
using Microsoft.Extensions.Configuration;
88
using Microsoft.Extensions.DependencyInjection;
9+
using static Microsoft.AspNetCore.Mvc.CompatibilityVersion;
910

1011
public class Startup
1112
{
@@ -19,10 +20,15 @@ public Startup( IConfiguration configuration )
1920
// This method gets called by the runtime. Use this method to add services to the container.
2021
public void ConfigureServices( IServiceCollection services )
2122
{
22-
services.AddMvc();
23-
24-
// reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
25-
services.AddApiVersioning( options => options.ReportApiVersions = true );
23+
// the sample application always uses the latest version, but you may want an explict version such as Version_2_2
24+
// note: Endpoint Routing is enabled by default; however, it is unsupported by OData and MUST be false
25+
services.AddMvc( options => options.EnableEndpointRouting = false ).SetCompatibilityVersion( Latest );
26+
services.AddApiVersioning(
27+
options =>
28+
{
29+
// reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
30+
options.ReportApiVersions = true;
31+
} );
2632
services.AddOData().EnableApiVersioning();
2733
}
2834

samples/aspnetcore/SwaggerODataSample/Startup.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.IO;
1414
using System.Reflection;
1515
using static Microsoft.AspNet.OData.Query.AllowedQueryOptions;
16+
using static Microsoft.AspNetCore.Mvc.CompatibilityVersion;
1617

1718
/// <summary>
1819
/// Represents the startup process for the application.
@@ -25,7 +26,9 @@ public class Startup
2526
/// <param name="services">The collection of services to configure the application with.</param>
2627
public void ConfigureServices( IServiceCollection services )
2728
{
28-
services.AddMvc();
29+
// the sample application always uses the latest version, but you may want an explict version such as Version_2_2
30+
// note: Endpoint Routing is enabled by default; however, it is unsupported by OData and MUST be false
31+
services.AddMvc( options => options.EnableEndpointRouting = false ).SetCompatibilityVersion( Latest );
2932
services.AddApiVersioning( options => options.ReportApiVersions = true );
3033
services.AddOData().EnableApiVersioning();
3134
services.AddODataApiExplorer(
@@ -107,7 +110,7 @@ static Info CreateInfoForApiVersion( ApiVersionDescription description )
107110
{
108111
var info = new Info()
109112
{
110-
Title = $"Sample API {description.ApiVersion}",
113+
Title = "Sample API",
111114
Version = description.ApiVersion.ToString(),
112115
Description = "A sample application with Swagger, Swashbuckle, and API versioning.",
113116
Contact = new Contact() { Name = "Bill Mei", Email = "bill.mei@somewhere.com" },

samples/aspnetcore/SwaggerSample/Startup.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Swashbuckle.AspNetCore.Swagger;
1010
using System.IO;
1111
using System.Reflection;
12+
using static Microsoft.AspNetCore.Mvc.CompatibilityVersion;
1213

1314
/// <summary>
1415
/// Represents the startup process for the application.
@@ -36,20 +37,26 @@ public Startup( IConfiguration configuration )
3637
/// <param name="services">The collection of services to configure the application with.</param>
3738
public void ConfigureServices( IServiceCollection services )
3839
{
39-
// add the versioned api explorer, which also adds IApiVersionDescriptionProvider service
40-
// note: the specified format code will format the version as "'v'major[.minor][-status]"
40+
// the sample application always uses the latest version, but you may want an explict version such as Version_2_2
41+
// note: Endpoint Routing is enabled by default; however, if you need legacy style routing via IRouter, change it to false
42+
services.AddMvc( options => options.EnableEndpointRouting = true ).SetCompatibilityVersion( Latest );
43+
services.AddApiVersioning(
44+
options =>
45+
{
46+
// reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
47+
options.ReportApiVersions = true;
48+
} );
4149
services.AddVersionedApiExplorer(
4250
options =>
4351
{
52+
// add the versioned api explorer, which also adds IApiVersionDescriptionProvider service
53+
// note: the specified format code will format the version as "'v'major[.minor][-status]"
4454
options.GroupNameFormat = "'v'VVV";
4555

4656
// note: this option is only necessary when versioning by url segment. the SubstitutionFormat
4757
// can also be used to control the format of the API version in route templates
4858
options.SubstituteApiVersionInUrl = true;
4959
} );
50-
51-
services.AddMvc();
52-
services.AddApiVersioning( options => options.ReportApiVersions = true );
5360
services.AddSwaggerGen(
5461
options =>
5562
{
@@ -110,7 +117,7 @@ static Info CreateInfoForApiVersion( ApiVersionDescription description )
110117
{
111118
var info = new Info()
112119
{
113-
Title = $"Sample API {description.ApiVersion}",
120+
Title = "Sample API",
114121
Version = description.ApiVersion.ToString(),
115122
Description = "A sample application with Swagger, Swashbuckle, and API versioning.",
116123
Contact = new Contact() { Name = "Bill Mei", Email = "bill.mei@somewhere.com" },

samples/aspnetcore/SwaggerSample/V1/Controllers/OrdersController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class OrdersController : ControllerBase
2020
/// <response code="200">The order was successfully retrieved.</response>
2121
/// <response code="404">The order does not exist.</response>
2222
[HttpGet( "{id:int}", Name = "GetOrderById" )]
23+
[Produces( "application/json" )]
2324
[ProducesResponseType( typeof( Order ), 200 )]
2425
[ProducesResponseType( 404 )]
2526
public IActionResult Get( int id ) => Ok( new Order() { Id = id, Customer = "John Doe" } );
@@ -33,6 +34,7 @@ public class OrdersController : ControllerBase
3334
/// <response code="400">The order is invalid.</response>
3435
[HttpPost]
3536
[MapToApiVersion( "1.0" )]
37+
[Produces( "application/json" )]
3638
[ProducesResponseType( typeof( Order ), 201 )]
3739
[ProducesResponseType( 400 )]
3840
public IActionResult Post( [FromBody] Order order )

samples/aspnetcore/SwaggerSample/V1/Controllers/PeopleController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class PeopleController : ControllerBase
2121
/// <response code="200">The person was successfully retrieved.</response>
2222
/// <response code="404">The person does not exist.</response>
2323
[HttpGet( "{id:int}" )]
24+
[Produces( "application/json" )]
2425
[ProducesResponseType( typeof( Person ), 200 )]
2526
[ProducesResponseType( 404 )]
2627
public IActionResult Get( int id ) =>

samples/aspnetcore/SwaggerSample/V2/Controllers/OrdersController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class OrdersController : ControllerBase
2121
/// <returns>All available orders.</returns>
2222
/// <response code="200">The successfully retrieved orders.</response>
2323
[HttpGet]
24+
[Produces( "application/json" )]
2425
[ProducesResponseType( typeof( IEnumerable<Order> ), 200 )]
2526
public IActionResult Get()
2627
{
@@ -42,6 +43,7 @@ public IActionResult Get()
4243
/// <response code="200">The order was successfully retrieved.</response>
4344
/// <response code="404">The order does not exist.</response>
4445
[HttpGet( "{id:int}", Name = ByIdRouteName )]
46+
[Produces( "application/json" )]
4547
[ProducesResponseType( typeof( Order ), 200 )]
4648
[ProducesResponseType( 400 )]
4749
[ProducesResponseType( 404 )]
@@ -55,6 +57,7 @@ public IActionResult Get()
5557
/// <response code="201">The order was successfully placed.</response>
5658
/// <response code="400">The order is invalid.</response>
5759
[HttpPost]
60+
[Produces( "application/json" )]
5861
[ProducesResponseType( typeof( Order ), 201 )]
5962
[ProducesResponseType( 400 )]
6063
public IActionResult Post( [FromBody] Order order )

samples/aspnetcore/SwaggerSample/V2/Controllers/PeopleController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class PeopleController : ControllerBase
2121
/// <returns>All available people.</returns>
2222
/// <response code="200">The successfully retrieved people.</response>
2323
[HttpGet]
24+
[Produces( "application/json" )]
2425
[ProducesResponseType( typeof( IEnumerable<Person> ), 200 )]
2526
public IActionResult Get()
2627
{
@@ -60,6 +61,7 @@ public IActionResult Get()
6061
/// <response code="200">The person was successfully retrieved.</response>
6162
/// <response code="404">The person does not exist.</response>
6263
[HttpGet( "{id:int}", Name = ByIdRouteName )]
64+
[Produces( "application/json" )]
6365
[ProducesResponseType( typeof( Person ), 200 )]
6466
[ProducesResponseType( 404 )]
6567
public IActionResult Get( int id ) =>

0 commit comments

Comments
 (0)