Skip to content

Make the default route constraints linkable #24727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Http/Routing/src/ParameterPolicyActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -86,6 +87,7 @@ public static T ResolveParameterPolicy<T>(
}
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", Justification = "This type comes from the ConstraintMap.")]
private static IParameterPolicy CreateParameterPolicy(IServiceProvider serviceProvider, Type parameterPolicyType, string argumentString)
{
ConstructorInfo activationConstructor = null;
Expand Down
72 changes: 40 additions & 32 deletions src/Http/Routing/src/RouteOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Routing.Constraints;

namespace Microsoft.AspNetCore.Routing
Expand Down Expand Up @@ -82,38 +83,45 @@ public IDictionary<string, Type> ConstraintMap

private static IDictionary<string, Type> GetDefaultConstraintMap()
{
return new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase)
{
// Type-specific constraints
{ "int", typeof(IntRouteConstraint) },
{ "bool", typeof(BoolRouteConstraint) },
{ "datetime", typeof(DateTimeRouteConstraint) },
{ "decimal", typeof(DecimalRouteConstraint) },
{ "double", typeof(DoubleRouteConstraint) },
{ "float", typeof(FloatRouteConstraint) },
{ "guid", typeof(GuidRouteConstraint) },
{ "long", typeof(LongRouteConstraint) },

// Length constraints
{ "minlength", typeof(MinLengthRouteConstraint) },
{ "maxlength", typeof(MaxLengthRouteConstraint) },
{ "length", typeof(LengthRouteConstraint) },

// Min/Max value constraints
{ "min", typeof(MinRouteConstraint) },
{ "max", typeof(MaxRouteConstraint) },
{ "range", typeof(RangeRouteConstraint) },

// Regex-based constraints
{ "alpha", typeof(AlphaRouteConstraint) },
{ "regex", typeof(RegexInlineRouteConstraint) },

{"required", typeof(RequiredRouteConstraint) },

// Files
{ "file", typeof(FileNameRouteConstraint) },
{ "nonfile", typeof(NonFileNameRouteConstraint) },
};
var defaults = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);

// Type-specific constraints
AddConstraint<IntRouteConstraint>(defaults, "int");
AddConstraint<BoolRouteConstraint>(defaults, "bool");
AddConstraint<DateTimeRouteConstraint>(defaults, "datetime");
AddConstraint<DecimalRouteConstraint>(defaults, "decimal");
AddConstraint<DoubleRouteConstraint>(defaults, "double");
AddConstraint<FloatRouteConstraint>(defaults, "float");
AddConstraint<GuidRouteConstraint>(defaults, "guid");
AddConstraint<LongRouteConstraint>(defaults, "long");

// Length constraints
AddConstraint<MinLengthRouteConstraint>(defaults, "minlength");
AddConstraint<MaxLengthRouteConstraint>(defaults, "maxlength");
AddConstraint<LengthRouteConstraint>(defaults, "length");

// Min/Max value constraints
AddConstraint<MinRouteConstraint>(defaults, "min");
AddConstraint<MaxRouteConstraint>(defaults, "max");
AddConstraint<RangeRouteConstraint>(defaults, "range");

// Regex-based constraints
AddConstraint<AlphaRouteConstraint>(defaults, "alpha");
AddConstraint<RegexInlineRouteConstraint>(defaults, "regex");

AddConstraint<RequiredRouteConstraint>(defaults, "required");

// Files
AddConstraint<FileNameRouteConstraint>(defaults, "file");
AddConstraint<NonFileNameRouteConstraint>(defaults, "nonfile");

return defaults;
}

// This API could be exposed on RouteOptions
private static void AddConstraint<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]TConstraint>(Dictionary<string, Type> constraintMap, string text) where TConstraint : IRouteConstraint
{
constraintMap[text] = typeof(TConstraint);
}
}
}