Skip to content

Commit 362157c

Browse files
authored
Merge pull request #809 from dotnet-maestro-bot/merge/release/2.2-to-master
[automated] Merge branch 'release/2.2' => 'master'
2 parents 0d44670 + b642db5 commit 362157c

12 files changed

+572
-201
lines changed

src/Microsoft.AspNetCore.Routing.Abstractions/LinkGenerationTemplate.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Routing
99
/// Defines a contract to generate a URL from a template.
1010
/// </summary>
1111
/// <remarks>
12-
/// A <see cref="LinkGenerationTemplate"/> can be created from <see cref="LinkGenerator.GetTemplateByAddress{TAddress}(TAddress)"/>
12+
/// A <see cref="LinkGenerationTemplate"/> can be created from <see cref="LinkGenerator.GetTemplateByAddress{TAddress}(TAddress, LinkGenerationTemplateOptions)"/>
1313
/// by supplying an address value which has matching endpoints. The returned <see cref="LinkGenerationTemplate"/>
1414
/// will be bound to the endpoints matching the address that was originally provided.
1515
/// </remarks>
@@ -20,6 +20,9 @@ public abstract class LinkGenerationTemplate
2020
/// </summary>
2121
/// <param name="httpContext">The <see cref="HttpContext"/> associated with the current request.</param>
2222
/// <param name="values">The route values. Used to expand parameters in the route template. Optional.</param>
23+
/// <param name="pathBase">
24+
/// An optional URI path base. Prepended to the path in the resulting URI. If not provided, the value of <see cref="HttpRequest.PathBase"/> will be used.
25+
/// </param>
2326
/// <param name="fragment">An optional URI fragment. Appended to the resulting URI.</param>
2427
/// <param name="options">
2528
/// An optional <see cref="LinkOptions"/>. Settings on provided object override the settings with matching
@@ -29,6 +32,7 @@ public abstract class LinkGenerationTemplate
2932
public abstract string GetPath(
3033
HttpContext httpContext,
3134
object values,
35+
PathString? pathBase = default,
3236
FragmentString fragment = default,
3337
LinkOptions options = default);
3438

@@ -54,6 +58,15 @@ public abstract string GetPath(
5458
/// </summary>
5559
/// <param name="httpContext">The <see cref="HttpContext"/> associated with the current request.</param>
5660
/// <param name="values">The route values. Used to expand parameters in the route template. Optional.</param>
61+
/// <param name="scheme">
62+
/// The URI scheme, applied to the resulting URI. Optional. If not provided, the value of <see cref="HttpRequest.Scheme"/> will be used.
63+
/// </param>
64+
/// <param name="host">
65+
/// The URI host/authority, applied to the resulting URI. Optional. If not provided, the value <see cref="HttpRequest.Host"/> will be used.
66+
/// </param>
67+
/// <param name="pathBase">
68+
/// An optional URI path base. Prepended to the path in the resulting URI. If not provided, the value of <see cref="HttpRequest.PathBase"/> will be used.
69+
/// </param>
5770
/// <param name="fragment">An optional URI fragment. Appended to the resulting URI.</param>
5871
/// <param name="options">
5972
/// An optional <see cref="LinkOptions"/>. Settings on provided object override the settings with matching
@@ -63,6 +76,9 @@ public abstract string GetPath(
6376
public abstract string GetUri(
6477
HttpContext httpContext,
6578
object values,
79+
string scheme = default,
80+
HostString? host = default,
81+
PathString? pathBase = default,
6682
FragmentString fragment = default,
6783
LinkOptions options = default);
6884

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.AspNetCore.Routing
5+
{
6+
/// <summary>
7+
/// Contains options for creating a <see cref="LinkGenerationTemplate" />.
8+
/// </summary>
9+
public class LinkGenerationTemplateOptions
10+
{
11+
/// <summary>
12+
/// Gets or sets a value indicating whether the template will use route values from the current request
13+
/// when generating a URI.
14+
/// </summary>
15+
public bool UseAmbientValues { get; set; }
16+
}
17+
}

src/Microsoft.AspNetCore.Routing.Abstractions/LinkGenerator.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ namespace Microsoft.AspNetCore.Routing
2525
public abstract class LinkGenerator
2626
{
2727
/// <summary>
28-
/// Generates a URI with an absolute path based on the provided values.
28+
/// Generates a URI with an absolute path based on the provided values and <see cref="HttpContext"/>.
2929
/// </summary>
3030
/// <typeparam name="TAddress">The address type.</typeparam>
3131
/// <param name="httpContext">The <see cref="HttpContext"/> associated with the current request.</param>
3232
/// <param name="address">The address value. Used to resolve endpoints.</param>
3333
/// <param name="values">The route values. Used to expand parameters in the route template. Optional.</param>
34+
/// <param name="ambientValues">The values associated with the current request. Optional.</param>
35+
/// <param name="pathBase">
36+
/// An optional URI path base. Prepended to the path in the resulting URI. If not provided, the value of <see cref="HttpRequest.PathBase"/> will be used.
37+
/// </param>
3438
/// <param name="fragment">An optional URI fragment. Appended to the resulting URI.</param>
3539
/// <param name="options">
3640
/// An optional <see cref="LinkOptions"/>. Settings on provided object override the settings with matching
@@ -41,6 +45,8 @@ public abstract string GetPathByAddress<TAddress>(
4145
HttpContext httpContext,
4246
TAddress address,
4347
RouteValueDictionary values,
48+
RouteValueDictionary ambientValues = default,
49+
PathString? pathBase = default,
4450
FragmentString fragment = default,
4551
LinkOptions options = default);
4652

@@ -65,12 +71,22 @@ public abstract string GetPathByAddress<TAddress>(
6571
LinkOptions options = default);
6672

6773
/// <summary>
68-
/// Generates an absolute URI based on the provided values.
74+
/// Generates an absolute URI based on the provided values and <see cref="HttpContext"/>.
6975
/// </summary>
7076
/// <typeparam name="TAddress">The address type.</typeparam>
7177
/// <param name="httpContext">The <see cref="HttpContext"/> associated with the current request.</param>
7278
/// <param name="address">The address value. Used to resolve endpoints.</param>
7379
/// <param name="values">The route values. Used to expand parameters in the route template. Optional.</param>
80+
/// <param name="ambientValues">The values associated with the current request. Optional.</param>
81+
/// <param name="scheme">
82+
/// The URI scheme, applied to the resulting URI. Optional. If not provided, the value of <see cref="HttpRequest.Scheme"/> will be used.
83+
/// </param>
84+
/// <param name="host">
85+
/// The URI host/authority, applied to the resulting URI. Optional. If not provided, the value <see cref="HttpRequest.Host"/> will be used.
86+
/// </param>
87+
/// <param name="pathBase">
88+
/// An optional URI path base. Prepended to the path in the resulting URI. If not provided, the value of <see cref="HttpRequest.PathBase"/> will be used.
89+
/// </param>
7490
/// <param name="fragment">An optional URI fragment. Appended to the resulting URI.</param>
7591
/// <param name="options">
7692
/// An optional <see cref="LinkOptions"/>. Settings on provided object override the settings with matching
@@ -81,6 +97,10 @@ public abstract string GetUriByAddress<TAddress>(
8197
HttpContext httpContext,
8298
TAddress address,
8399
RouteValueDictionary values,
100+
RouteValueDictionary ambientValues = default,
101+
string scheme = default,
102+
HostString? host = default,
103+
PathString? pathBase = default,
84104
FragmentString fragment = default,
85105
LinkOptions options = default);
86106

@@ -113,9 +133,10 @@ public abstract string GetUriByAddress<TAddress>(
113133
/// </summary>
114134
/// <typeparam name="TAddress">The address type.</typeparam>
115135
/// <param name="address">The address value. Used to resolve endpoints.</param>
136+
/// <param name="options">Options for the created <see cref="LinkGenerationTemplate"/>.</param>
116137
/// <returns>
117138
/// A <see cref="LinkGenerationTemplate"/> if one or more endpoints matching the address can be found, otherwise <c>null</c>.
118139
/// </returns>
119-
public abstract LinkGenerationTemplate GetTemplateByAddress<TAddress>(TAddress address);
140+
public abstract LinkGenerationTemplate GetTemplateByAddress<TAddress>(TAddress address, LinkGenerationTemplateOptions options = null);
120141
}
121142
}
Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using Microsoft.AspNetCore.Http;
45
using System;
56
using System.Collections.Generic;
6-
using Microsoft.AspNetCore.Http;
77

88
namespace Microsoft.AspNetCore.Routing
99
{
1010
internal sealed class DefaultLinkGenerationTemplate : LinkGenerationTemplate
1111
{
12-
public DefaultLinkGenerationTemplate(DefaultLinkGenerator linkGenerator, List<RouteEndpoint> endpoints)
12+
public DefaultLinkGenerationTemplate(DefaultLinkGenerator linkGenerator, List<RouteEndpoint> endpoints, LinkGenerationTemplateOptions options)
1313
{
1414
LinkGenerator = linkGenerator;
1515
Endpoints = endpoints;
16+
Options = options;
1617
}
1718

1819
public DefaultLinkGenerator LinkGenerator { get; }
1920

2021
public List<RouteEndpoint> Endpoints { get; }
2122

23+
public LinkGenerationTemplateOptions Options { get; }
24+
2225
public override string GetPath(
2326
HttpContext httpContext,
2427
object values,
28+
PathString? pathBase = default,
2529
FragmentString fragment = default,
26-
LinkOptions options = null)
30+
LinkOptions options = default)
2731
{
2832
if (httpContext == null)
2933
{
@@ -32,9 +36,9 @@ public override string GetPath(
3236

3337
return LinkGenerator.GetPathByEndpoints(
3438
Endpoints,
35-
DefaultLinkGenerator.GetAmbientValues(httpContext),
3639
new RouteValueDictionary(values),
37-
httpContext.Request.PathBase,
40+
GetAmbientValues(httpContext),
41+
pathBase ?? httpContext.Request.PathBase,
3842
fragment,
3943
options);
4044
}
@@ -43,22 +47,25 @@ public override string GetPath(
4347
object values,
4448
PathString pathBase = default,
4549
FragmentString fragment = default,
46-
LinkOptions options = null)
50+
LinkOptions options = default)
4751
{
4852
return LinkGenerator.GetPathByEndpoints(
4953
Endpoints,
50-
ambientValues: null,
5154
new RouteValueDictionary(values),
52-
pathBase,
53-
fragment,
54-
options);
55+
ambientValues: null,
56+
pathBase: pathBase,
57+
fragment: fragment,
58+
options: options);
5559
}
5660

5761
public override string GetUri(
5862
HttpContext httpContext,
5963
object values,
64+
string scheme = default,
65+
HostString? host = default,
66+
PathString? pathBase = default,
6067
FragmentString fragment = default,
61-
LinkOptions options = null)
68+
LinkOptions options = default)
6269
{
6370
if (httpContext == null)
6471
{
@@ -67,11 +74,11 @@ public override string GetUri(
6774

6875
return LinkGenerator.GetUriByEndpoints(
6976
Endpoints,
70-
DefaultLinkGenerator.GetAmbientValues(httpContext),
7177
new RouteValueDictionary(values),
72-
httpContext.Request.Scheme,
73-
httpContext.Request.Host,
74-
httpContext.Request.PathBase,
78+
GetAmbientValues(httpContext),
79+
scheme ?? httpContext.Request.Scheme,
80+
host ?? httpContext.Request.Host,
81+
pathBase ?? httpContext.Request.PathBase,
7582
fragment,
7683
options);
7784
}
@@ -82,17 +89,32 @@ public override string GetUri(
8289
HostString host,
8390
PathString pathBase = default,
8491
FragmentString fragment = default,
85-
LinkOptions options = null)
92+
LinkOptions options = default)
8693
{
94+
if (string.IsNullOrEmpty(scheme))
95+
{
96+
throw new ArgumentException("A scheme must be provided.", nameof(scheme));
97+
}
98+
99+
if (!host.HasValue)
100+
{
101+
throw new ArgumentException("A host must be provided.", nameof(host));
102+
}
103+
87104
return LinkGenerator.GetUriByEndpoints(
88105
Endpoints,
89-
ambientValues: null,
90106
new RouteValueDictionary(values),
91-
scheme,
92-
host,
93-
pathBase,
94-
fragment,
95-
options);
107+
ambientValues: null,
108+
scheme: scheme,
109+
host: host,
110+
pathBase: pathBase,
111+
fragment: fragment,
112+
options: options);
113+
}
114+
115+
private RouteValueDictionary GetAmbientValues(HttpContext httpContext)
116+
{
117+
return (Options?.UseAmbientValues ?? false) ? DefaultLinkGenerator.GetAmbientValues(httpContext) : null;
96118
}
97119
}
98120
}

0 commit comments

Comments
 (0)