-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
RequiredRouteConstraint.cs
38 lines (33 loc) · 1.2 KB
/
RequiredRouteConstraint.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Globalization;
using Microsoft.AspNetCore.Http;
namespace Microsoft.AspNetCore.Routing.Constraints;
/// <summary>
/// Constraints a route parameter that must have a value.
/// </summary>
/// <remarks>
/// This constraint is primarily used to enforce that a non-parameter value is present during
/// URL generation.
/// </remarks>
public class RequiredRouteConstraint : IRouteConstraint
{
/// <inheritdoc />
public bool Match(
HttpContext? httpContext,
IRouter? route,
string routeKey,
RouteValueDictionary values,
RouteDirection routeDirection)
{
ArgumentNullException.ThrowIfNull(routeKey);
ArgumentNullException.ThrowIfNull(values);
if (values.TryGetValue(routeKey, out var value) && value != null)
{
// In routing the empty string is equivalent to null, which is equivalent to an unset value.
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
return !string.IsNullOrEmpty(valueString);
}
return false;
}
}