|
1 |
| -// Copyright (c) .NET Foundation. All rights reserved. |
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
3 | 3 |
|
4 | 4 | using System;
|
| 5 | +using System.Collections; |
5 | 6 | using System.Collections.Generic;
|
6 | 7 | using System.Collections.ObjectModel;
|
7 | 8 | using System.Linq;
|
@@ -51,6 +52,7 @@ public static RoutePattern Parse(string pattern)
|
51 | 52 | /// Additional parameter policies to associated with the route pattern. May be null.
|
52 | 53 | /// The provided object will be converted to key-value pairs using <see cref="RouteValueDictionary"/>
|
53 | 54 | /// and then merged into the parsed route pattern.
|
| 55 | + /// Multiple policies can be specified for a key by providing a collection as the value. |
54 | 56 | /// </param>
|
55 | 57 | /// <returns>The <see cref="RoutePattern"/>.</returns>
|
56 | 58 | public static RoutePattern Parse(string pattern, object defaults, object parameterPolicies)
|
@@ -78,6 +80,7 @@ public static RoutePattern Parse(string pattern, object defaults, object paramet
|
78 | 80 | /// Additional parameter policies to associated with the route pattern. May be null.
|
79 | 81 | /// The provided object will be converted to key-value pairs using <see cref="RouteValueDictionary"/>
|
80 | 82 | /// and then merged into the parsed route pattern.
|
| 83 | + /// Multiple policies can be specified for a key by providing a collection as the value. |
81 | 84 | /// </param>
|
82 | 85 | /// <param name="requiredValues">
|
83 | 86 | /// Route values that can be substituted for parameters in the route pattern. See remarks on <see cref="RoutePattern.RequiredValues"/>.
|
@@ -138,6 +141,7 @@ public static RoutePattern Pattern(string rawText, IEnumerable<RoutePatternPathS
|
138 | 141 | /// Additional parameter policies to associated with the route pattern. May be null.
|
139 | 142 | /// The provided object will be converted to key-value pairs using <see cref="RouteValueDictionary"/>
|
140 | 143 | /// and then merged into the route pattern.
|
| 144 | + /// Multiple policies can be specified for a key by providing a collection as the value. |
141 | 145 | /// </param>
|
142 | 146 | /// <param name="segments">The collection of segments.</param>
|
143 | 147 | /// <returns>The <see cref="RoutePattern"/>.</returns>
|
@@ -168,6 +172,7 @@ public static RoutePattern Pattern(
|
168 | 172 | /// Additional parameter policies to associated with the route pattern. May be null.
|
169 | 173 | /// The provided object will be converted to key-value pairs using <see cref="RouteValueDictionary"/>
|
170 | 174 | /// and then merged into the route pattern.
|
| 175 | + /// Multiple policies can be specified for a key by providing a collection as the value. |
171 | 176 | /// </param>
|
172 | 177 | /// <param name="segments">The collection of segments.</param>
|
173 | 178 | /// <returns>The <see cref="RoutePattern"/>.</returns>
|
@@ -229,6 +234,7 @@ public static RoutePattern Pattern(string rawText, params RoutePatternPathSegmen
|
229 | 234 | /// Additional parameter policies to associated with the route pattern. May be null.
|
230 | 235 | /// The provided object will be converted to key-value pairs using <see cref="RouteValueDictionary"/>
|
231 | 236 | /// and then merged into the route pattern.
|
| 237 | + /// Multiple policies can be specified for a key by providing a collection as the value. |
232 | 238 | /// </param>
|
233 | 239 | /// <param name="segments">The collection of segments.</param>
|
234 | 240 | /// <returns>The <see cref="RoutePattern"/>.</returns>
|
@@ -259,6 +265,7 @@ public static RoutePattern Pattern(
|
259 | 265 | /// Additional parameter policies to associated with the route pattern. May be null.
|
260 | 266 | /// The provided object will be converted to key-value pairs using <see cref="RouteValueDictionary"/>
|
261 | 267 | /// and then merged into the route pattern.
|
| 268 | + /// Multiple policies can be specified for a key by providing a collection as the value. |
262 | 269 | /// </param>
|
263 | 270 | /// <param name="segments">The collection of segments.</param>
|
264 | 271 | /// <returns>The <see cref="RoutePattern"/>.</returns>
|
@@ -312,12 +319,33 @@ private static RoutePattern PatternCore(
|
312 | 319 |
|
313 | 320 | foreach (var kvp in parameterPolicies)
|
314 | 321 | {
|
315 |
| - updatedParameterPolicies.Add(kvp.Key, new List<RoutePatternParameterPolicyReference>() |
| 322 | + var policyReferences = new List<RoutePatternParameterPolicyReference>(); |
| 323 | + |
| 324 | + if (kvp.Value is IParameterPolicy parameterPolicy) |
| 325 | + { |
| 326 | + policyReferences.Add(ParameterPolicy(parameterPolicy)); |
| 327 | + } |
| 328 | + else if (kvp.Value is string) |
316 | 329 | {
|
317 |
| - kvp.Value is IParameterPolicy parameterPolicy |
318 |
| - ? ParameterPolicy(parameterPolicy) |
319 |
| - : Constraint(kvp.Value), // Constraint will convert string values into regex constraints |
320 |
| - }); |
| 330 | + // Constraint will convert string values into regex constraints |
| 331 | + policyReferences.Add(Constraint(kvp.Value)); |
| 332 | + } |
| 333 | + else if (kvp.Value is IEnumerable multiplePolicies) |
| 334 | + { |
| 335 | + foreach (var item in multiplePolicies) |
| 336 | + { |
| 337 | + // Constraint will convert string values into regex constraints |
| 338 | + policyReferences.Add(item is IParameterPolicy p ? ParameterPolicy(p) : Constraint(item)); |
| 339 | + } |
| 340 | + } |
| 341 | + else |
| 342 | + { |
| 343 | + throw new InvalidOperationException(Resources.FormatRoutePattern_InvalidConstraintReference( |
| 344 | + kvp.Value ?? "null", |
| 345 | + typeof(IRouteConstraint))); |
| 346 | + } |
| 347 | + |
| 348 | + updatedParameterPolicies.Add(kvp.Key, policyReferences); |
321 | 349 | }
|
322 | 350 | }
|
323 | 351 |
|
|
0 commit comments