question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Blazor Custom Route Constraints

See original GitHub issue

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:

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:23
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
kyletinsleycommented, Jun 2, 2022

Regex route constraints would be wonderful. Trying to constrain a string route parameter to a finite list of string values and want to 404 otherwise.

2reactions
Alerinoscommented, Feb 19, 2022

@pranavkm I would like to remind you of this, it is quite an important and useful thing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Constraining route parameters
It's not currently possible to constrain a parameter to match a value of an enum. Custom constraints. It is not possible to define...
Read more >
ASP.NET Core Blazor routing and navigation
Route templates · Focus an element on navigation · Provide custom content when content isn't found · Route to components from multiple assemblies ......
Read more >
Can you customize route constraints in blazor routing
Accepting Henk's answer: No, currently not possible without writing a custom router... use "OnParameterSet".
Read more >
How do you implement the routing constraints in Blazor?
A route constraint enforces type matching on a route segment to a component. In the following example, the route to the Users component...
Read more >
Creating Custom Constraints For Razor Pages Routes
Razor Page already includes a wide range of constraints that can be used to help disambiguate routes. It is also possible for you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found