-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
MapRouteRouteBuilderExtensions.cs
171 lines (154 loc) · 7.32 KB
/
MapRouteRouteBuilderExtensions.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable enable
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Constraints;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Builder;
/// <summary>
/// Provides extension methods for <see cref="IRouteBuilder"/> to add routes.
/// </summary>
public static class MapRouteRouteBuilderExtensions
{
/// <summary>
/// Adds a route to the <see cref="IRouteBuilder"/> with the specified name and template.
/// </summary>
/// <param name="routeBuilder">The <see cref="IRouteBuilder"/> to add the route to.</param>
/// <param name="name">The name of the route.</param>
/// <param name="template">The URL pattern of the route.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
[RequiresUnreferencedCode("This API may perform reflection on supplied parameter which may be trimmed if not referenced directly.")]
public static IRouteBuilder MapRoute(
this IRouteBuilder routeBuilder,
string? name,
[StringSyntax("Route")] string? template)
{
MapRoute(routeBuilder, name, template, defaults: null);
return routeBuilder;
}
/// <summary>
/// Adds a route to the <see cref="IRouteBuilder"/> with the specified name, template, and default values.
/// </summary>
/// <param name="routeBuilder">The <see cref="IRouteBuilder"/> to add the route to.</param>
/// <param name="name">The name of the route.</param>
/// <param name="template">The URL pattern of the route.</param>
/// <param name="defaults">
/// An object that contains default values for route parameters. The object's properties represent the names
/// and values of the default values.
/// </param>
/// <returns>A reference to this instance after the operation has completed.</returns>
[RequiresUnreferencedCode("This API may perform reflection on supplied parameter which may be trimmed if not referenced directly.")]
public static IRouteBuilder MapRoute(
this IRouteBuilder routeBuilder,
string? name,
[StringSyntax("Route")] string? template,
object? defaults)
{
return MapRoute(routeBuilder, name, template, defaults, constraints: null);
}
/// <summary>
/// Adds a route to the <see cref="IRouteBuilder"/> with the specified name, template, default values, and
/// constraints.
/// </summary>
/// <param name="routeBuilder">The <see cref="IRouteBuilder"/> to add the route to.</param>
/// <param name="name">The name of the route.</param>
/// <param name="template">The URL pattern of the route.</param>
/// <param name="defaults">
/// An object that contains default values for route parameters. The object's properties represent the names
/// and values of the default values.
/// </param>
/// <param name="constraints">
/// An object that contains constraints for the route. The object's properties represent the names and values
/// of the constraints.
/// </param>
/// <returns>A reference to this instance after the operation has completed.</returns>
[RequiresUnreferencedCode("This API may perform reflection on supplied parameter which may be trimmed if not referenced directly.")]
public static IRouteBuilder MapRoute(
this IRouteBuilder routeBuilder,
string? name,
[StringSyntax("Route")] string? template,
object? defaults,
object? constraints)
{
return MapRoute(routeBuilder, name, template, defaults, constraints, dataTokens: null);
}
/// <summary>
/// Adds a route to the <see cref="IRouteBuilder"/> with the specified name, template, default values, and
/// data tokens.
/// </summary>
/// <param name="routeBuilder">The <see cref="IRouteBuilder"/> to add the route to.</param>
/// <param name="name">The name of the route.</param>
/// <param name="template">The URL pattern of the route.</param>
/// <param name="defaults">
/// An object that contains default values for route parameters. The object's properties represent the names
/// and values of the default values.
/// </param>
/// <param name="constraints">
/// An object that contains constraints for the route. The object's properties represent the names and values
/// of the constraints.
/// </param>
/// <param name="dataTokens">
/// An object that contains data tokens for the route. The object's properties represent the names and values
/// of the data tokens.
/// </param>
/// <returns>A reference to this instance after the operation has completed.</returns>
[RequiresUnreferencedCode("This API may perform reflection on supplied parameter which may be trimmed if not referenced directly.")]
public static IRouteBuilder MapRoute(
this IRouteBuilder routeBuilder,
string? name,
[StringSyntax("Route")] string? template,
object? defaults,
object? constraints,
object? dataTokens)
{
if (routeBuilder.DefaultHandler == null)
{
throw new RouteCreationException(Resources.FormatDefaultHandler_MustBeSet(nameof(IRouteBuilder)));
}
routeBuilder.Routes.Add(new Route(
routeBuilder.DefaultHandler,
name,
template,
new RouteValueDictionary(defaults),
new RouteValueDictionary(constraints)!,
new RouteValueDictionary(dataTokens),
CreateInlineConstraintResolver(routeBuilder.ServiceProvider)));
return routeBuilder;
}
private static IInlineConstraintResolver CreateInlineConstraintResolver(IServiceProvider serviceProvider)
{
var inlineConstraintResolver = serviceProvider
.GetRequiredService<IInlineConstraintResolver>();
var parameterPolicyFactory = serviceProvider
.GetRequiredService<ParameterPolicyFactory>();
// This inline constraint resolver will return a null constraint for non-IRouteConstraint
// parameter policies so Route does not error
return new BackCompatInlineConstraintResolver(inlineConstraintResolver, parameterPolicyFactory);
}
private sealed class BackCompatInlineConstraintResolver : IInlineConstraintResolver
{
private readonly IInlineConstraintResolver _inner;
private readonly ParameterPolicyFactory _parameterPolicyFactory;
public BackCompatInlineConstraintResolver(IInlineConstraintResolver inner, ParameterPolicyFactory parameterPolicyFactory)
{
_inner = inner;
_parameterPolicyFactory = parameterPolicyFactory;
}
public IRouteConstraint? ResolveConstraint(string inlineConstraint)
{
var routeConstraint = _inner.ResolveConstraint(inlineConstraint);
if (routeConstraint != null)
{
return routeConstraint;
}
var parameterPolicy = _parameterPolicyFactory.Create(null!, inlineConstraint);
if (parameterPolicy != null)
{
// Logic inside Route will skip adding NullRouteConstraint
return NullRouteConstraint.Instance;
}
return null;
}
}
}