-
Notifications
You must be signed in to change notification settings - Fork 709
/
Copy pathApiVersionLinkGenerator.cs
102 lines (90 loc) · 3.89 KB
/
ApiVersionLinkGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
namespace Microsoft.AspNetCore.Mvc.Routing
{
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Versioning;
using Microsoft.AspNetCore.Routing;
using System;
/// <summary>
/// Represents an API version aware <see cref="AspNetCore.Routing.LinkGenerator">link generator</see>.
/// </summary>
[CLSCompliant( false )]
public class ApiVersionLinkGenerator : LinkGenerator
{
/// <summary>
/// Initializes a new instance of the <see cref="ApiVersionLinkGenerator"/> class.
/// </summary>
/// <param name="linkGenerator">The inner <see cref="AspNetCore.Routing.LinkGenerator">link generator</see>.</param>
public ApiVersionLinkGenerator( LinkGenerator linkGenerator ) => LinkGenerator = linkGenerator;
/// <summary>
/// Gets the inner link generator.
/// </summary>
/// <value>The inner <see cref="AspNetCore.Routing.LinkGenerator">link generator</see>.</value>
protected LinkGenerator LinkGenerator { get; }
/// <inheritdoc />
public override string? GetPathByAddress<TAddress>(
HttpContext httpContext,
TAddress address,
RouteValueDictionary values,
RouteValueDictionary? ambientValues = null,
PathString? pathBase = null,
FragmentString fragment = default,
LinkOptions? options = null )
{
AddApiVersionRouteValueIfNecessary( httpContext, values );
return LinkGenerator.GetPathByAddress( httpContext, address, values, ambientValues, pathBase, fragment, options );
}
/// <inheritdoc />
public override string? GetPathByAddress<TAddress>(
TAddress address,
RouteValueDictionary values,
PathString pathBase = default,
FragmentString fragment = default,
LinkOptions? options = null ) => LinkGenerator.GetPathByAddress( address, values, pathBase, fragment, options );
/// <inheritdoc />
public override string? GetUriByAddress<TAddress>(
HttpContext httpContext,
TAddress address,
RouteValueDictionary values,
RouteValueDictionary? ambientValues = null,
string? scheme = null,
HostString? host = null,
PathString? pathBase = null,
FragmentString fragment = default,
LinkOptions? options = null )
{
AddApiVersionRouteValueIfNecessary( httpContext, values );
return LinkGenerator.GetUriByAddress( httpContext, address, values, ambientValues, scheme, host, pathBase, fragment, options );
}
/// <inheritdoc />
public override string? GetUriByAddress<TAddress>(
TAddress address,
RouteValueDictionary values,
string scheme,
HostString host,
PathString pathBase = default,
FragmentString fragment = default,
LinkOptions? options = null ) => LinkGenerator.GetUriByAddress( address, values, scheme, host, pathBase, fragment, options );
static void AddApiVersionRouteValueIfNecessary( HttpContext httpContext, RouteValueDictionary values )
{
if ( httpContext == null )
{
throw new ArgumentNullException( nameof( httpContext ) );
}
if ( values == null )
{
throw new ArgumentNullException( nameof( values ) );
}
var feature = httpContext.Features.Get<IApiVersioningFeature>();
var key = feature.RouteParameter;
if ( string.IsNullOrEmpty( key ) )
{
return;
}
var value = feature.RawRequestedApiVersion;
if ( !string.IsNullOrEmpty( value ) && !values.ContainsKey( key ) )
{
values.Add( key, value );
}
}
}
}