Source/Constraints/RouteConstraint.cs
/// <summary>
/// Creates a structured RouteConstraint object given a string that contains the route constraint. A constraint
/// is the place after the colon in a parameter definition, for example `{age:int?}`. If the constraint denotes
/// an optional, this method will return an <see cref="OptionalTypeRouteConstraint{T}"/> which handles the
/// appropriate checks.
/// </summary>
/// <param name="constraint">String representation of the constraint</param>
/// <returns>Type-specific RouteConstraint object</returns>
Creates a structured RouteConstraint object given a string that contains the route constraint. A constraint
is the place after the colon in a parameter definition, for example `{age:int?}`. If the constraint denotes
an optional, this method will return an  which handles the
appropriate checks.
Creates a structured RouteConstraint object given a string that contains the route constraint . A constraint is the place after the colon in a parameter definition for example ` { age : int? } ` . If the constraint denotes an optional this method will return an which handles the appropriate checks .
private static RouteConstraint CreateRouteConstraint(string constraint, string name)
        {
            switch (constraint)
            {
                case "bool":
                    return new TypeRouteConstraint<bool>(bool.TryParse, name);
                case "bool?":
                    return new OptionalTypeRouteConstraint<bool>(bool.TryParse, name);
                case "datetime":
                    return new TypeRouteConstraint<DateTime>((string str, out DateTime result)
                        => DateTime.TryParse(str, CultureInfo.InvariantCulture, DateTimeStyles.None, out result), name);
                case "datetime?":
                    return new OptionalTypeRouteConstraint<DateTime>((string str, out DateTime result)
                        => DateTime.TryParse(str, CultureInfo.InvariantCulture, DateTimeStyles.None, out result), name);
                case "decimal":
                    return new TypeRouteConstraint<decimal>((string str, out decimal result)
                        => decimal.TryParse(str, NumberStyles.Number, CultureInfo.InvariantCulture, out result), name);
                case "decimal?":
                    return new OptionalTypeRouteConstraint<decimal>((string str, out decimal result)
                        => decimal.TryParse(str, NumberStyles.Number, CultureInfo.InvariantCulture, out result), name);
                case "double":
                    return new TypeRouteConstraint<double>((string str, out double result)
                        => double.TryParse(str, NumberStyles.Number, CultureInfo.InvariantCulture, out result), name);
                case "double?":
                    return new OptionalTypeRouteConstraint<double>((string str, out double result)
                        => double.TryParse(str, NumberStyles.Number, CultureInfo.InvariantCulture, out result), name);
                case "float":
                    return new TypeRouteConstraint<float>((string str, out float result)
                        => float.TryParse(str, NumberStyles.Number, CultureInfo.InvariantCulture, out result), name);
                case "float?":
                    return new OptionalTypeRouteConstraint<float>((string str, out float result)
                        => float.TryParse(str, NumberStyles.Number, CultureInfo.InvariantCulture, out result), name);
                case "guid":
                    return new TypeRouteConstraint<Guid>(Guid.TryParse, name);
                case "guid?":
                    return new OptionalTypeRouteConstraint<Guid>(Guid.TryParse, name);
                case "int":
                    return new TypeRouteConstraint<int>((string str, out int result)
                        => int.TryParse(str, NumberStyles.Integer, CultureInfo.InvariantCulture, out result), name);
                case "int?":
                    return new OptionalTypeRouteConstraint<int>((string str, out int result)
                        => int.TryParse(str, NumberStyles.Integer, CultureInfo.InvariantCulture, out result), name);
                case "long":
                    return new TypeRouteConstraint<long>((string str, out long result)
                        => long.TryParse(str, NumberStyles.Integer, CultureInfo.InvariantCulture, out result), name);
                case "long?":
                    return new OptionalTypeRouteConstraint<long>((string str, out long result)
                        => long.TryParse(str, NumberStyles.Integer, CultureInfo.InvariantCulture, out result), name);
                default:
                    return null;
            }
        }
private static RouteConstraint CreateRouteConstraint ( string constraint , string name ) { switch ( constraint ) { case " " : return new TypeRouteConstraint < bool > ( bool . TryParse , name ) ; case " " : return new OptionalTypeRouteConstraint < bool > ( bool . TryParse , name ) ; case " " : return new TypeRouteConstraint < DateTime > ( ( string str , out DateTime result ) => DateTime . TryParse ( str , CultureInfo . InvariantCulture , DateTimeStyles . None , out result ) , name ) ; case " " : return new OptionalTypeRouteConstraint < DateTime > ( ( string str , out DateTime result ) => DateTime . TryParse ( str , CultureInfo . InvariantCulture , DateTimeStyles . None , out result ) , name ) ; case " " : return new TypeRouteConstraint < decimal > ( ( string str , out decimal result ) => decimal . TryParse ( str , NumberStyles . Number , CultureInfo . InvariantCulture , out result ) , name ) ; case " " : return new OptionalTypeRouteConstraint < decimal > ( ( string str , out decimal result ) => decimal . TryParse ( str , NumberStyles . Number , CultureInfo . InvariantCulture , out result ) , name ) ; case " " : return new TypeRouteConstraint < double > ( ( string str , out double result ) => double . TryParse ( str , NumberStyles . Number , CultureInfo . InvariantCulture , out result ) , name ) ; case " " : return new OptionalTypeRouteConstraint < double > ( ( string str , out double result ) => double . TryParse ( str , NumberStyles . Number , CultureInfo . InvariantCulture , out result ) , name ) ; case " " : return new TypeRouteConstraint < float > ( ( string str , out float result ) => float . TryParse ( str , NumberStyles . Number , CultureInfo . InvariantCulture , out result ) , name ) ; case " " : return new OptionalTypeRouteConstraint < float > ( ( string str , out float result ) => float . TryParse ( str , NumberStyles . Number , CultureInfo . InvariantCulture , out result ) , name ) ; case " " : return new TypeRouteConstraint < Guid > ( Guid . TryParse , name ) ; case " " : return new OptionalTypeRouteConstraint < Guid > ( Guid . TryParse , name ) ; case " " : return new TypeRouteConstraint < int > ( ( string str , out int result ) => int . TryParse ( str , NumberStyles . Integer , CultureInfo . InvariantCulture , out result ) , name ) ; case " " : return new OptionalTypeRouteConstraint < int > ( ( string str , out int result ) => int . TryParse ( str , NumberStyles . Integer , CultureInfo . InvariantCulture , out result ) , name ) ; case " " : return new TypeRouteConstraint < long > ( ( string str , out long result ) => long . TryParse ( str , NumberStyles . Integer , CultureInfo . InvariantCulture , out result ) , name ) ; case " " : return new OptionalTypeRouteConstraint < long > ( ( string str , out long result ) => long . TryParse ( str , NumberStyles . Integer , CultureInfo . InvariantCulture , out result ) , name ) ; default : return null ; } }
Creates a structured RouteConstraint object given a string that contains the route constraint.
Creates a structured RouteConstraint object given a string that contains the route constraint .
Type-specific RouteConstraint object
Type - specific RouteConstraint object
constraint
string
String representation of the constraint
String representation of the constraint
name
string