Skip to content

Commit 65a6c8e

Browse files
Complete update web API examples
1 parent f128b32 commit 65a6c8e

6 files changed

Lines changed: 26 additions & 335 deletions

File tree

examples/AspNetCore/WebApi/MinimalApiExample/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Asp.Versioning;
2+
13
var builder = WebApplication.CreateBuilder( args );
24

35
// Add services to the container.
@@ -36,7 +38,7 @@
3638
var v2 = forecast.MapGroup( "/weatherforecast" )
3739
.HasApiVersion( 2.0 );
3840

39-
v2.MapGet( "/", () =>
41+
v2.MapGet( "/", ( ApiVersion version ) =>
4042
{
4143
return Enumerable.Range( 0, summaries.Length ).Select( index =>
4244
new WeatherForecast

examples/AspNetCore/WebApi/MinimalOpenApiExample/ConfigureSwaggerOptions.cs

Lines changed: 0 additions & 162 deletions
This file was deleted.

examples/AspNetCore/WebApi/MinimalOpenApiExample/Program.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@
1717
// "api-supported-versions" and "api-deprecated-versions"
1818
options.ReportApiVersions = true;
1919

20+
options.Policies.Deprecate( 0.9 )
21+
.Effective( DateTimeOffset.Now )
22+
.Link( "policy.html" )
23+
.Title( "Version Deprecation Policy" )
24+
.Type( "text/html" );
25+
2026
options.Policies.Sunset( 0.9 )
2127
.Effective( DateTimeOffset.Now.AddDays( 60 ) )
2228
.Link( "policy.html" )
23-
.Title( "Versioning Policy" )
29+
.Title( "Version Sunset Policy" )
2430
.Type( "text/html" );
2531
} )
2632
.AddApiExplorer(
@@ -34,10 +40,7 @@
3440
// can also be used to control the format of the API version in route templates
3541
options.SubstituteApiVersionInUrl = true;
3642
} )
37-
.AddOpenApi( options => options.Document.AddScalarTransformers() )
38-
// this enables binding ApiVersion as a endpoint callback parameter. if you don't use it, then
39-
// you should remove this configuration.
40-
.EnableApiVersionBinding();
43+
.AddOpenApi( options => options.Document.AddScalarTransformers() );
4144

4245
var app = builder.Build();
4346

examples/AspNetCore/WebApi/MinimalOpenApiExample/Services/Orders.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace ApiVersioning.Examples.Services;
1+
using Asp.Versioning;
2+
3+
namespace ApiVersioning.Examples.Services;
24

35
/// <summary>
46
/// Provides the endpoint extensions for the Orders service.
@@ -95,7 +97,7 @@ public void ToV3()
9597

9698
api.MapPost( "/", V3.Post )
9799
.Accepts( orders.ModelType, "application/json" )
98-
.Produces( 201 , orders.ModelType )
100+
.Produces( 201, orders.ModelType )
99101
.Produces( 400 );
100102

101103
api.MapDelete( "/{id:int}", V3.Delete )
@@ -158,9 +160,10 @@ public static class V2
158160
/// Get Orders
159161
/// </summary>
160162
/// <description>Retrieves all orders.</description>
163+
/// <param name="version"></param>
161164
/// <returns>All available orders.</returns>
162165
/// <response code="200">The successfully retrieved orders.</response>
163-
public static Models.V2.Order[] GetAll() =>
166+
public static Models.V2.Order[] GetAll( ApiVersion version ) =>
164167
[
165168
new (){ Id = 1, Customer = "John Doe" },
166169
new (){ Id = 2, Customer = "Bob Smith" },
@@ -172,10 +175,11 @@ public static Models.V2.Order[] GetAll() =>
172175
/// </summary>
173176
/// <description>Gets a single order.</description>
174177
/// <param name="id">The requested order identifier.</param>
178+
/// <param name="version"></param>
175179
/// <returns>The requested order.</returns>
176180
/// <response code="200">The order was successfully retrieved.</response>
177181
/// <response code="404">The order does not exist.</response>
178-
public static Models.V2.Order GetById( int id ) => new() { Id = id, Customer = "John Doe" };
182+
public static Models.V2.Order GetById( int id, ApiVersion version ) => new() { Id = id, Customer = "John Doe" };
179183

180184
/// <summary>
181185
/// Place Order

0 commit comments

Comments
 (0)