Skip to content
This repository was archived by the owner on Nov 27, 2018. It is now read-only.

Commit fbffcb7

Browse files
committed
Adding Remaining Inline Route Constraints.
1 parent 0ca5576 commit fbffcb7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1828
-84
lines changed

samples/RoutingSample.Web/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ await context
4545
routeBuilder.MapRoute("regexStringRoute",
4646
"api/rconstraint/{controller}",
4747
new { foo = "Bar" },
48-
new { controller = new RegexConstraint("^(my.*)$") });
48+
new { controller = new RegexRouteConstraint("^(my.*)$") });
4949
routeBuilder.MapRoute("regexRoute",
5050
"api/r2constraint/{controller}",
5151
new { foo = "Bar2" },
52-
new { controller = new RegexConstraint(new Regex("^(my.*)$")) });
52+
new { controller = new RegexRouteConstraint(new Regex("^(my.*)$")) });
5353

5454
routeBuilder.MapRoute("parameterConstraintRoute",
5555
"api/{controller}/{*extra}",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. 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.AspNet.Routing.Constraints
5+
{
6+
/// <summary>
7+
/// Constrains a route parameter to contain only lowercase or uppercase letters A through Z in the English alphabet.
8+
/// </summary>
9+
public class AlphaRouteConstraint : RegexRouteConstraint
10+
{
11+
/// <summary>
12+
/// Initializes a new instance of the <see cref="AlphaRouteConstraint" /> class.
13+
/// </summary>
14+
public AlphaRouteConstraint() : base(@"^[a-z]*$")
15+
{
16+
}
17+
}
18+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Globalization;
7+
using Microsoft.AspNet.Http;
8+
9+
namespace Microsoft.AspNet.Routing.Constraints
10+
{
11+
/// <summary>
12+
/// Constrains a route parameter to represent only Boolean values.
13+
/// </summary>
14+
public class BoolRouteConstraint : IRouteConstraint
15+
{
16+
/// <inheritdoc />
17+
public bool Match([NotNull] HttpContext httpContext,
18+
[NotNull] IRouter route,
19+
[NotNull] string routeKey,
20+
[NotNull] IDictionary<string, object> values,
21+
RouteDirection routeDirection)
22+
{
23+
object value;
24+
if (values.TryGetValue(routeKey, out value) && value != null)
25+
{
26+
if (value is bool)
27+
{
28+
return true;
29+
}
30+
31+
bool result;
32+
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
33+
return Boolean.TryParse(valueString, out result);
34+
}
35+
36+
return false;
37+
}
38+
}
39+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Globalization;
7+
using Microsoft.AspNet.Http;
8+
9+
namespace Microsoft.AspNet.Routing.Constraints
10+
{
11+
/// <summary>
12+
/// Constrains a route parameter to represent only <see cref="DateTime"/> values.
13+
/// Supports date time formats represented by CultureInfo.DateTimeFormat for the CultureInfo.InvariantCulture.
14+
/// </summary>
15+
public class DateTimeRouteConstraint : IRouteConstraint
16+
{
17+
/// <inheritdoc />
18+
public bool Match([NotNull] HttpContext httpContext,
19+
[NotNull] IRouter route,
20+
[NotNull] string routeKey,
21+
[NotNull] IDictionary<string, object> values,
22+
RouteDirection routeDirection)
23+
{
24+
object value;
25+
if (values.TryGetValue(routeKey, out value) && value != null)
26+
{
27+
if (value is DateTime)
28+
{
29+
return true;
30+
}
31+
32+
DateTime result;
33+
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
34+
return DateTime.TryParse(valueString, CultureInfo.InvariantCulture, DateTimeStyles.None, out result);
35+
}
36+
37+
return false;
38+
}
39+
}
40+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Globalization;
7+
using Microsoft.AspNet.Http;
8+
9+
namespace Microsoft.AspNet.Routing.Constraints
10+
{
11+
/// <summary>
12+
/// Constrains a route parameter to represent only decimal values.
13+
/// </summary>
14+
public class DecimalRouteConstraint : IRouteConstraint
15+
{
16+
/// <inheritdoc />
17+
public bool Match([NotNull] HttpContext httpContext,
18+
[NotNull] IRouter route,
19+
[NotNull] string routeKey,
20+
[NotNull] IDictionary<string, object> values,
21+
RouteDirection routeDirection)
22+
{
23+
object value;
24+
if (values.TryGetValue(routeKey, out value) && value != null)
25+
{
26+
if (value is decimal)
27+
{
28+
return true;
29+
}
30+
31+
decimal result;
32+
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
33+
return Decimal.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out result);
34+
}
35+
36+
return false;
37+
}
38+
}
39+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Globalization;
7+
using Microsoft.AspNet.Http;
8+
9+
namespace Microsoft.AspNet.Routing.Constraints
10+
{
11+
/// <summary>
12+
/// Constrains a route parameter to represent only 64-bit floating-point values.
13+
/// </summary>
14+
public class DoubleRouteConstraint : IRouteConstraint
15+
{
16+
/// <inheritdoc />
17+
public bool Match([NotNull] HttpContext httpContext,
18+
[NotNull] IRouter route,
19+
[NotNull] string routeKey,
20+
[NotNull] IDictionary<string, object> values,
21+
RouteDirection routeDirection)
22+
{
23+
object value;
24+
if (values.TryGetValue(routeKey, out value) && value != null)
25+
{
26+
if (value is double)
27+
{
28+
return true;
29+
}
30+
31+
double result;
32+
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
33+
return Double.TryParse(valueString,
34+
NumberStyles.Float | NumberStyles.AllowThousands,
35+
CultureInfo.InvariantCulture,
36+
out result);
37+
}
38+
return false;
39+
}
40+
}
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Globalization;
7+
using Microsoft.AspNet.Http;
8+
9+
namespace Microsoft.AspNet.Routing.Constraints
10+
{
11+
/// <summary>
12+
/// Constrains a route parameter to represent only 32-bit floating-point values.
13+
/// </summary>
14+
public class FloatRouteConstraint : IRouteConstraint
15+
{
16+
/// <inheritdoc />
17+
public bool Match([NotNull] HttpContext httpContext,
18+
[NotNull] IRouter route,
19+
[NotNull] string routeKey,
20+
[NotNull] IDictionary<string, object> values,
21+
RouteDirection routeDirection)
22+
{
23+
object value;
24+
if (values.TryGetValue(routeKey, out value) && value != null)
25+
{
26+
if (value is float)
27+
{
28+
return true;
29+
}
30+
31+
float result;
32+
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
33+
return Single.TryParse(valueString,
34+
NumberStyles.Float | NumberStyles.AllowThousands,
35+
CultureInfo.InvariantCulture,
36+
out result);
37+
}
38+
39+
return false;
40+
}
41+
}
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Globalization;
7+
using Microsoft.AspNet.Http;
8+
9+
namespace Microsoft.AspNet.Routing.Constraints
10+
{
11+
/// <summary>
12+
/// Constrains a route parameter to represent only <see cref="Guid"/> values.
13+
/// Matches values specified in any of the five formats "N", "D", "B", "P", or "X",
14+
/// supported by Guid.ToString(string) and Guid.ToString(String, IFormatProvider) methods.
15+
/// </summary>
16+
public class GuidRouteConstraint : IRouteConstraint
17+
{
18+
/// <inheritdoc />
19+
public bool Match([NotNull] HttpContext httpContext,
20+
[NotNull] IRouter route,
21+
[NotNull] string routeKey,
22+
[NotNull] IDictionary<string, object> values,
23+
RouteDirection routeDirection)
24+
{
25+
object value;
26+
if (values.TryGetValue(routeKey, out value) && value != null)
27+
{
28+
if (value is Guid)
29+
{
30+
return true;
31+
}
32+
33+
Guid result;
34+
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
35+
return Guid.TryParse(valueString, out result);
36+
}
37+
38+
return false;
39+
}
40+
}
41+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Globalization;
7+
using Microsoft.AspNet.Http;
8+
9+
namespace Microsoft.AspNet.Routing.Constraints
10+
{
11+
/// <summary>
12+
/// Constrains a route parameter to be a string of a given length or within a given range of lengths.
13+
/// </summary>
14+
public class LengthRouteConstraint : IRouteConstraint
15+
{
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="LengthRouteConstraint" /> class that constrains
18+
/// a route parameter to be a string of a given length.
19+
/// </summary>
20+
/// <param name="length">The length of the route parameter.</param>
21+
public LengthRouteConstraint(int length)
22+
{
23+
if (length < 0)
24+
{
25+
var errorMessage = Resources.FormatArgumentMustBeGreaterThanOrEqualTo(0);
26+
throw new ArgumentOutOfRangeException("length", length, errorMessage);
27+
}
28+
29+
MinLength = MaxLength = length;
30+
}
31+
32+
/// <summary>
33+
/// Initializes a new instance of the <see cref="LengthRouteConstraint" /> class that constrains
34+
/// a route parameter to be a string of a given length.
35+
/// </summary>
36+
/// <param name="minLength">The minimum length allowed for the route parameter.</param>
37+
/// <param name="maxLength">The maximum length allowed for the route parameter.</param>
38+
public LengthRouteConstraint(int minLength, int maxLength)
39+
{
40+
if (minLength < 0)
41+
{
42+
var errorMessage = Resources.FormatArgumentMustBeGreaterThanOrEqualTo(0);
43+
throw new ArgumentOutOfRangeException("minLength", minLength, errorMessage);
44+
}
45+
46+
if (maxLength < 0)
47+
{
48+
var errorMessage = Resources.FormatArgumentMustBeGreaterThanOrEqualTo(0);
49+
throw new ArgumentOutOfRangeException("maxLength", maxLength, errorMessage);
50+
}
51+
52+
if (minLength > maxLength)
53+
{
54+
var errorMessage =
55+
Resources.FormatRangeConstraint_MinShouldBeLessThanOrEqualToMax("minLength", "maxLength");
56+
throw new ArgumentOutOfRangeException("minLength", minLength, errorMessage);
57+
}
58+
59+
MinLength = minLength;
60+
MaxLength = maxLength;
61+
}
62+
63+
/// <summary>
64+
/// Gets the minimum length allowed for the route parameter.
65+
/// </summary>
66+
public int MinLength { get; private set; }
67+
68+
/// <summary>
69+
/// Gets the maximum length allowed for the route parameter.
70+
/// </summary>
71+
public int MaxLength { get; private set; }
72+
73+
/// <inheritdoc />
74+
public bool Match([NotNull] HttpContext httpContext,
75+
[NotNull] IRouter route,
76+
[NotNull] string routeKey,
77+
[NotNull] IDictionary<string, object> values,
78+
RouteDirection routeDirection)
79+
{
80+
object value;
81+
if (values.TryGetValue(routeKey, out value) && value != null)
82+
{
83+
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
84+
var length = valueString.Length;
85+
return length >= MinLength && length <= MaxLength;
86+
}
87+
88+
return false;
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)