Skip to content

Commit e36fb1b

Browse files
Add version selector to API Explorer option. Relates to #1025
1 parent 4c969b1 commit e36fb1b

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/AspNet/WebApi/src/Asp.Versioning.WebApi.ApiExplorer/ApiExplorer/ApiExplorerOptions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,14 @@ public partial class ApiExplorerOptions
4444
/// </summary>
4545
/// <value>The name associated with the <see cref="ApiVersionRouteConstraint">API version route constraint</see>.</value>
4646
public string RouteConstraintName => options.Value.RouteConstraintName;
47+
48+
/// <summary>
49+
/// Gets or sets the API version selector.
50+
/// </summary>
51+
/// <value>An <see cref="IApiVersionSelector">API version selector</see> object.</value>
52+
public IApiVersionSelector ApiVersionSelector
53+
{
54+
get => apiVersionSelector ?? options.Value.ApiVersionSelector;
55+
set => apiVersionSelector = value;
56+
}
4757
}

src/AspNetCore/WebApi/src/Asp.Versioning.Mvc.ApiExplorer/ApiExplorerOptions.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Asp.Versioning.ApiExplorer;
44

55
using Asp.Versioning.Routing;
6+
using Microsoft.AspNetCore.Http;
67

78
/// <content>
89
/// Provides additional implementation specific to ASP.NET Core.
@@ -37,7 +38,7 @@ public ApiVersion DefaultApiVersion
3738
/// <value>The <see cref="IApiVersionParameterSource">API version parameter source</see> used to describe API version parameters.</value>
3839
public IApiVersionParameterSource ApiVersionParameterSource
3940
{
40-
get => parameterSource ??= ApiVersionReader.Combine( new QueryStringApiVersionReader(), new UrlSegmentApiVersionReader() );
41+
get => parameterSource ??= ApiVersionReader.Default;
4142
set => parameterSource = value;
4243
}
4344

@@ -47,6 +48,17 @@ public IApiVersionParameterSource ApiVersionParameterSource
4748
/// <value>The name associated with the <see cref="ApiVersionRouteConstraint">API version route constraint</see>.</value>
4849
public string RouteConstraintName { get; set; } = string.Empty;
4950

51+
/// <summary>
52+
/// Gets or sets the API version selector.
53+
/// </summary>
54+
/// <value>An <see cref="IApiVersionSelector">API version selector</see> object.</value>
55+
[CLSCompliant( false )]
56+
public IApiVersionSelector ApiVersionSelector
57+
{
58+
get => apiVersionSelector ??= new DefaultApiVersionSelector( this );
59+
set => apiVersionSelector = value;
60+
}
61+
5062
/// <summary>
5163
/// Gets or sets the function used to format the combination of a group name and API version.
5264
/// </summary>
@@ -55,4 +67,13 @@ public IApiVersionParameterSource ApiVersionParameterSource
5567
/// <remarks>The specified callback will only be invoked if a group name has been configured. The API
5668
/// version will be provided formatted according to the <see cref="GroupNameFormat">group name format</see>.</remarks>
5769
public FormatGroupNameCallback? FormatGroupName { get; set; }
70+
71+
private sealed class DefaultApiVersionSelector : IApiVersionSelector
72+
{
73+
private readonly ApiExplorerOptions options;
74+
75+
public DefaultApiVersionSelector( ApiExplorerOptions options ) => this.options = options;
76+
77+
public ApiVersion SelectVersion( HttpRequest request, ApiVersionModel model ) => options.DefaultApiVersion;
78+
}
5879
}

src/Common/src/Common.ApiExplorer/ApiExplorerOptions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22

33
namespace Asp.Versioning.ApiExplorer;
44

5+
#if NETFRAMEWORK
6+
using HttpRequest = System.Net.Http.HttpRequestMessage;
7+
#else
8+
using Microsoft.AspNetCore.Http;
9+
#endif
10+
511
/// <summary>
612
/// Represents the possible API versioning options for the API explorer.
713
/// </summary>
814
public partial class ApiExplorerOptions
915
{
16+
private IApiVersionSelector? apiVersionSelector;
17+
1018
/// <summary>
1119
/// Gets or sets the format used to create group names from API versions.
1220
/// </summary>

0 commit comments

Comments
 (0)