Skip to content

Commit e3b7040

Browse files
authored
Revisions to LinkGenerator (#770)
* Revisions to LinkGenerator * PR feedback
1 parent 950d2e9 commit e3b7040

22 files changed

+2759
-2159
lines changed

benchmarks/Microsoft.AspNetCore.Routing.Performance/LinkGeneration/LinkGenerationGithubBenchmark.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ public void TreeRouter()
5757
[Benchmark]
5858
public void EndpointRouting()
5959
{
60-
var actualUrl = _linkGenerator.GetLink(
60+
var actualUrl = _linkGenerator.GetPathByRouteValues(
6161
_requestContext.HttpContext,
62-
values: new RouteValueDictionary(_lookUpValues));
62+
routeName: null,
63+
values: _lookUpValues);
6364

6465
AssertUrl("/repos/aspnet/routing/issues/comments/20202", actualUrl);
6566
}

benchmarks/Microsoft.AspNetCore.Routing.Performance/LinkGeneration/SingleRouteWithConstraintsBenchmark.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ public void TreeRouter()
5656
[Benchmark]
5757
public void EndpointRouting()
5858
{
59-
var actualUrl = _linkGenerator.GetLink(
59+
var actualUrl = _linkGenerator.GetPathByRouteValues(
6060
_requestContext.HttpContext,
61-
values: new RouteValueDictionary(
62-
new
63-
{
64-
controller = "Customers",
65-
action = "Details",
66-
category = "Administration",
67-
region = "US",
68-
id = 10
69-
}));
61+
routeName: null,
62+
values: new
63+
{
64+
controller = "Customers",
65+
action = "Details",
66+
category = "Administration",
67+
region = "US",
68+
id = 10
69+
});
7070

7171
AssertUrl("/Customers/Details/Administration/US/10", actualUrl);
7272
}

benchmarks/Microsoft.AspNetCore.Routing.Performance/LinkGeneration/SingleRouteWithNoParametersBenchmark.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ public void TreeRouter()
5353
[Benchmark]
5454
public void EndpointRouting()
5555
{
56-
var actualUrl = _linkGenerator.GetLink(
56+
var actualUrl = _linkGenerator.GetPathByRouteValues(
5757
_requestContext.HttpContext,
58-
values: new RouteValueDictionary(
59-
new
60-
{
61-
controller = "Products",
62-
action = "Details",
63-
}));
58+
routeName: null,
59+
values: new
60+
{
61+
controller = "Products",
62+
action = "Details",
63+
});
6464

6565
AssertUrl("/Products/Details", actualUrl);
6666
}

benchmarks/Microsoft.AspNetCore.Routing.Performance/LinkGeneration/SingleRouteWithParametersBenchmark.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ public void TreeRouter()
5656
[Benchmark]
5757
public void EndpointRouting()
5858
{
59-
var actualUrl = _linkGenerator.GetLink(
59+
var actualUrl = _linkGenerator.GetPathByRouteValues(
6060
_requestContext.HttpContext,
61-
values: new RouteValueDictionary(
62-
new
63-
{
64-
controller = "Customers",
65-
action = "Details",
66-
category = "Administration",
67-
region = "US",
68-
id = 10
69-
}));
61+
routeName: null,
62+
values: new
63+
{
64+
controller = "Customers",
65+
action = "Details",
66+
category = "Administration",
67+
region = "US",
68+
id = 10
69+
});
7070

7171
AssertUrl("/Customers/Details/Administration/US/10", actualUrl);
7272
}

samples/RoutingSample.Web/UseEndpointRoutingStartup.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ public void ConfigureServices(IServiceCollection services)
103103
response.StatusCode = 200;
104104
response.ContentType = "text/plain";
105105
return response.WriteAsync(
106-
"Link: " + linkGenerator.GetLink(httpContext, "WithSingleAsteriskCatchAll", new { }));
106+
"Link: " + linkGenerator.GetPathByRouteValues(httpContext, "WithSingleAsteriskCatchAll", new { }));
107107
},
108108
RoutePatternFactory.Parse("/WithSingleAsteriskCatchAll/{*path}"),
109109
0,
110110
new EndpointMetadataCollection(
111111
new RouteValuesAddressMetadata(
112-
name: "WithSingleAsteriskCatchAll",
112+
routeName: "WithSingleAsteriskCatchAll",
113113
requiredValues: new RouteValueDictionary())),
114114
"WithSingleAsteriskCatchAll"),
115115
new RouteEndpoint((httpContext) =>
@@ -120,13 +120,13 @@ public void ConfigureServices(IServiceCollection services)
120120
response.StatusCode = 200;
121121
response.ContentType = "text/plain";
122122
return response.WriteAsync(
123-
"Link: " + linkGenerator.GetLink(httpContext, "WithDoubleAsteriskCatchAll", new { }));
123+
"Link: " + linkGenerator.GetPathByRouteValues(httpContext, "WithDoubleAsteriskCatchAll", new { }));
124124
},
125125
RoutePatternFactory.Parse("/WithDoubleAsteriskCatchAll/{**path}"),
126126
0,
127127
new EndpointMetadataCollection(
128128
new RouteValuesAddressMetadata(
129-
name: "WithDoubleAsteriskCatchAll",
129+
routeName: "WithDoubleAsteriskCatchAll",
130130
requiredValues: new RouteValueDictionary())),
131131
"WithDoubleAsteriskCatchAll"),
132132
});
Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,90 @@
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;
5+
46
namespace Microsoft.AspNetCore.Routing
57
{
68
/// <summary>
79
/// Defines a contract to generate a URL from a template.
810
/// </summary>
11+
/// <remarks>
12+
/// A <see cref="LinkGenerationTemplate"/> can be created from <see cref="LinkGenerator.GetTemplateByAddress{TAddress}(TAddress)"/>
13+
/// by supplying an address value which has matching endpoints. The returned <see cref="LinkGenerationTemplate"/>
14+
/// will be bound to the endpoints matching the address that was originally provided.
15+
/// </remarks>
916
public abstract class LinkGenerationTemplate
1017
{
1118
/// <summary>
12-
/// Generates a URL with an absolute path from the specified route values.
19+
/// Generates a URI with an absolute path based on the provided values.
20+
/// </summary>
21+
/// <param name="httpContext">The <see cref="HttpContext"/> associated with the current request.</param>
22+
/// <param name="values">The route values. Used to expand parameters in the route template. Optional.</param>
23+
/// <param name="fragment">An optional URI fragment. Appended to the resulting URI.</param>
24+
/// <param name="options">
25+
/// An optional <see cref="LinkOptions"/>. Settings on provided object override the settings with matching
26+
/// names from <c>RouteOptions</c>.
27+
/// </param>
28+
/// <returns>A URI with an absolute path, or <c>null</c>.</returns>
29+
public abstract string GetPath(
30+
HttpContext httpContext,
31+
object values,
32+
FragmentString fragment = default,
33+
LinkOptions options = default);
34+
35+
/// <summary>
36+
/// Generates a URI with an absolute path based on the provided values.
37+
/// </summary>
38+
/// <param name="values">The route values. Used to expand parameters in the route template. Optional.</param>
39+
/// <param name="pathBase">An optional URI path base. Prepended to the path in the resulting URI.</param>
40+
/// <param name="fragment">An optional URI fragment. Appended to the resulting URI.</param>
41+
/// <param name="options">
42+
/// An optional <see cref="LinkOptions"/>. Settings on provided object override the settings with matching
43+
/// names from <c>RouteOptions</c>.
44+
/// </param>
45+
/// <returns>A URI with an absolute path, or <c>null</c>.</returns>
46+
public abstract string GetPath(
47+
object values,
48+
PathString pathBase = default,
49+
FragmentString fragment = default,
50+
LinkOptions options = default);
51+
52+
/// <summary>
53+
/// Generates an absolute URI based on the provided values.
1354
/// </summary>
14-
/// <param name="values">An object that contains route values.</param>
15-
/// <returns>The generated URL.</returns>
16-
public string MakeUrl(object values)
17-
{
18-
return MakeUrl(values, options: null);
19-
}
55+
/// <param name="httpContext">The <see cref="HttpContext"/> associated with the current request.</param>
56+
/// <param name="values">The route values. Used to expand parameters in the route template. Optional.</param>
57+
/// <param name="fragment">An optional URI fragment. Appended to the resulting URI.</param>
58+
/// <param name="options">
59+
/// An optional <see cref="LinkOptions"/>. Settings on provided object override the settings with matching
60+
/// names from <c>RouteOptions</c>.
61+
/// </param>
62+
/// <returns>A URI with an absolute path, or <c>null</c>.</returns>
63+
public abstract string GetUri(
64+
HttpContext httpContext,
65+
object values,
66+
FragmentString fragment = default,
67+
LinkOptions options = default);
2068

2169
/// <summary>
22-
/// Generates a URL with an absolute path from the specified route values and link options.
70+
/// Generates an absolute URI based on the provided values.
2371
/// </summary>
24-
/// <param name="values">An object that contains route values.</param>
25-
/// <param name="options">The <see cref="LinkOptions"/>.</param>
26-
/// <returns>The generated URL.</returns>
27-
public abstract string MakeUrl(object values, LinkOptions options);
72+
/// <param name="values">The route values. Used to expand parameters in the route template. Optional.</param>
73+
/// <param name="scheme">The URI scheme, applied to the resulting URI.</param>
74+
/// <param name="host">The URI host/authority, applied to the resulting URI.</param>
75+
/// <param name="pathBase">An optional URI path base. Prepended to the path in the resulting URI.</param>
76+
/// <param name="fragment">An optional URI fragment. Appended to the resulting URI.</param>
77+
/// <param name="options">
78+
/// An optional <see cref="LinkOptions"/>. Settings on provided object override the settings with matching
79+
/// names from <c>RouteOptions</c>.
80+
/// </param>
81+
/// <returns>An absolute URI, or <c>null</c>.</returns>
82+
public abstract string GetUri(
83+
object values,
84+
string scheme,
85+
HostString host,
86+
PathString pathBase = default,
87+
FragmentString fragment = default,
88+
LinkOptions options = default);
2889
}
2990
}

0 commit comments

Comments
 (0)