Make the default route constraints linkable (#24727)
- Added a helper that gives the linker the ability to preserve the constructors of these types
This commit is contained in:
parent
983e7ed635
commit
fa0cb71186
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
@ -86,6 +87,7 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", Justification = "This type comes from the ConstraintMap.")]
|
||||||
private static IParameterPolicy CreateParameterPolicy(IServiceProvider serviceProvider, Type parameterPolicyType, string argumentString)
|
private static IParameterPolicy CreateParameterPolicy(IServiceProvider serviceProvider, Type parameterPolicyType, string argumentString)
|
||||||
{
|
{
|
||||||
ConstructorInfo activationConstructor = null;
|
ConstructorInfo activationConstructor = null;
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using Microsoft.AspNetCore.Routing.Constraints;
|
using Microsoft.AspNetCore.Routing.Constraints;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Routing
|
namespace Microsoft.AspNetCore.Routing
|
||||||
|
|
@ -82,38 +83,45 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
|
|
||||||
private static IDictionary<string, Type> GetDefaultConstraintMap()
|
private static IDictionary<string, Type> GetDefaultConstraintMap()
|
||||||
{
|
{
|
||||||
return new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase)
|
var defaults = 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
|
// Type-specific constraints
|
||||||
{ "minlength", typeof(MinLengthRouteConstraint) },
|
AddConstraint<IntRouteConstraint>(defaults, "int");
|
||||||
{ "maxlength", typeof(MaxLengthRouteConstraint) },
|
AddConstraint<BoolRouteConstraint>(defaults, "bool");
|
||||||
{ "length", typeof(LengthRouteConstraint) },
|
AddConstraint<DateTimeRouteConstraint>(defaults, "datetime");
|
||||||
|
AddConstraint<DecimalRouteConstraint>(defaults, "decimal");
|
||||||
|
AddConstraint<DoubleRouteConstraint>(defaults, "double");
|
||||||
|
AddConstraint<FloatRouteConstraint>(defaults, "float");
|
||||||
|
AddConstraint<GuidRouteConstraint>(defaults, "guid");
|
||||||
|
AddConstraint<LongRouteConstraint>(defaults, "long");
|
||||||
|
|
||||||
// Min/Max value constraints
|
// Length constraints
|
||||||
{ "min", typeof(MinRouteConstraint) },
|
AddConstraint<MinLengthRouteConstraint>(defaults, "minlength");
|
||||||
{ "max", typeof(MaxRouteConstraint) },
|
AddConstraint<MaxLengthRouteConstraint>(defaults, "maxlength");
|
||||||
{ "range", typeof(RangeRouteConstraint) },
|
AddConstraint<LengthRouteConstraint>(defaults, "length");
|
||||||
|
|
||||||
// Regex-based constraints
|
// Min/Max value constraints
|
||||||
{ "alpha", typeof(AlphaRouteConstraint) },
|
AddConstraint<MinRouteConstraint>(defaults, "min");
|
||||||
{ "regex", typeof(RegexInlineRouteConstraint) },
|
AddConstraint<MaxRouteConstraint>(defaults, "max");
|
||||||
|
AddConstraint<RangeRouteConstraint>(defaults, "range");
|
||||||
|
|
||||||
{"required", typeof(RequiredRouteConstraint) },
|
// Regex-based constraints
|
||||||
|
AddConstraint<AlphaRouteConstraint>(defaults, "alpha");
|
||||||
|
AddConstraint<RegexInlineRouteConstraint>(defaults, "regex");
|
||||||
|
|
||||||
// Files
|
AddConstraint<RequiredRouteConstraint>(defaults, "required");
|
||||||
{ "file", typeof(FileNameRouteConstraint) },
|
|
||||||
{ "nonfile", typeof(NonFileNameRouteConstraint) },
|
// 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue