dotnet / dotnet/aspnetcore

Blazor Custom Route Constraints

Open
#28,938 24 comments 43 reactions 1 assignee Claimed by @javiercn View on GitHub
affected-few area-blazor enhancement feature-blazor-builtin-components feature-routing on-roadmap Pillar: Complete Blazor Web severity-minor
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

I am trying to use custom route constraint in Blazor, similar to how it currently works for both [razor pages and MVC routes](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-5.0#custom-route-constraints):

```cs
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:

https://github.com/dotnet/aspnetcore/blob/af7c0cc2a2f944811041b55f9b4e0ae6cc948fd7/src/Components/Components/src/Routing/RouteConstraint.cs#L57-L85

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.