-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Open
Labels
Pillar: Complete Blazor Webaffected-fewThis issue impacts only small number of customersThis issue impacts only small number of customersarea-blazorIncludes: Blazor, Razor ComponentsIncludes: Blazor, Razor ComponentsenhancementThis issue represents an ask for new feature or an enhancement to an existing oneThis issue represents an ask for new feature or an enhancement to an existing onefeature-blazor-builtin-componentsFeatures related to the built in components we ship or could ship in the futureFeatures related to the built in components we ship or could ship in the futurefeature-routingseverity-minorThis label is used by an internal toolThis label is used by an internal tool
Milestone
Description
I am trying to use custom route constraint in Blazor, similar to how it currently works for both razor pages and MVC routes:
services.AddRouting(options =>
{
options.ConstraintMap.Add("customName", typeof(MyCustomConstraint));
});
... but it appears that blazor route constraints do not consider any custom constraints that have been registered:
aspnetcore/src/Components/Components/src/Routing/RouteConstraint.cs
Lines 57 to 85 in af7c0cc
private static RouteConstraint? CreateRouteConstraint(string constraint) | |
{ | |
switch (constraint) | |
{ | |
case "bool": | |
return new TypeRouteConstraint<bool>(bool.TryParse); | |
case "datetime": | |
return new TypeRouteConstraint<DateTime>((string str, out DateTime result) | |
=> DateTime.TryParse(str, CultureInfo.InvariantCulture, DateTimeStyles.None, out result)); | |
case "decimal": | |
return new TypeRouteConstraint<decimal>((string str, out decimal result) | |
=> decimal.TryParse(str, NumberStyles.Number, CultureInfo.InvariantCulture, out result)); | |
case "double": | |
return new TypeRouteConstraint<double>((string str, out double result) | |
=> double.TryParse(str, NumberStyles.Number, CultureInfo.InvariantCulture, out result)); | |
case "float": | |
return new TypeRouteConstraint<float>((string str, out float result) | |
=> float.TryParse(str, NumberStyles.Number, CultureInfo.InvariantCulture, out result)); | |
case "guid": | |
return new TypeRouteConstraint<Guid>(Guid.TryParse); | |
case "int": | |
return new TypeRouteConstraint<int>((string str, out int result) | |
=> int.TryParse(str, NumberStyles.Integer, CultureInfo.InvariantCulture, out result)); | |
case "long": | |
return new TypeRouteConstraint<long>((string str, out long result) | |
=> long.TryParse(str, NumberStyles.Integer, CultureInfo.InvariantCulture, out result)); | |
default: | |
return null; | |
} |
So I instead encounter the ArgumentException, "Unsupported constraint '{constraint}' in route '{template}'.".
It would be ideal if RouteConstraint
could be modified to reference RouteOptions.ContraintMap
as the other routing engines do. Alternatively, needing to use some Blazor-specific registration method to register constraints only for Blazor would also be acceptable.
devlife, Elekaene, Rene-Sackers, KrisVandermotten, nrhoffmann and 30 more
Metadata
Metadata
Assignees
Labels
Pillar: Complete Blazor Webaffected-fewThis issue impacts only small number of customersThis issue impacts only small number of customersarea-blazorIncludes: Blazor, Razor ComponentsIncludes: Blazor, Razor ComponentsenhancementThis issue represents an ask for new feature or an enhancement to an existing oneThis issue represents an ask for new feature or an enhancement to an existing onefeature-blazor-builtin-componentsFeatures related to the built in components we ship or could ship in the futureFeatures related to the built in components we ship or could ship in the futurefeature-routingseverity-minorThis label is used by an internal toolThis label is used by an internal tool