aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Blazor Custom Route Constraints

Open GravlLift opened this issue 4 years ago • 6 comments

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.

GravlLift avatar Dec 30 '20 19:12 GravlLift

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Jan 05 '21 17:01 ghost

Extensible route constraints should be a must (and I thought that was already solved in asp.net ~12/13 years ago). The SetParametersAsync parsing "hack" might work for some very contrived scenarios... but it completely disallows generalization, and even forbids doing certain scenarios without many more hacks...

Just imagine some route like: /users/{id}/{action:createOrEditConstraint} where you want one page to resolve both /create and /edit, but you want other different page to resolve /list (or others). Since you can't have user-defined constraints, /users/{id}/{action} matches both pages and you'll get an error.

Yes, it could be solved by creating -yet another component- and either redirect or include the create/edit component and list component in it, but that's yet another hack, and a ton more of boilerplate (one more component, with all its initialization and parsing per "CRUD" entity in a project).

One more scenario (which I used in razor pages/webapi/MVC a lot) is having short guids as parameters (while internally using Guid). Yes, you can just have a string and parse it on SetParametersAsync, but you can't generalize that (on a base class, for example), because you wouldn't have access to the inherited class parameter, so you basically need to specify the SetParameterAsync method and parse specifically on every single routable view with a short guid parameter (and again, since you can't constraint it -with a regex or something-, you can't distinguish it from other string routes).

Again, nothing that can't be "hacked away", but this is basic, and I'm sure many people (even those not on this thread) would miss this, so I believe affected-few doesn't look like a correct label here, to me.

javiercampos avatar Mar 04 '21 18:03 javiercampos

After 2 hours of debugging why my ulong constraint isn't working, I stumbled upon this bug. For me it looks like a very basic feature. Please fix this soon. Also ulong and uint should be added to the default constraints.

Tragen avatar Sep 22 '21 14:09 Tragen

I believe this should be introduced in ASP.NET Core 7

Alerinos avatar Oct 22 '21 02:10 Alerinos

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

Alerinos avatar Feb 19 '22 15:02 Alerinos

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.

kyletinsley avatar Jun 02 '22 21:06 kyletinsley

It'd be super useful if you depend on enum in routes, too. It's a case in my project https://stalcraftclan.com - and currently I have to work it around by grabbing string and manually trying to parse the value. It results in a lot of boilerplate code and is also quite error-prone.

It's supported in other ASP.NET Core areas, I don't see why it couldn't be in Blazor.

TehGM avatar Feb 28 '23 13:02 TehGM

This would be really really useful to have, for example when I want to allow just a couple string values (a enum basically). Right now it's very tedious and annoying code to write.

haefele avatar Apr 14 '23 13:04 haefele

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Jun 29 '23 22:06 ghost

It appears that folks are tone deaf. This is a requested requirement and now it appears not even 'short' is supported. I fail to understand why foundational issues like this keep getting put to the back burner.

kcabral817 avatar Aug 14 '23 00:08 kcabral817

Hello, I've faced with the same situation. I need to customize routing constraint and have no ways to do this. Is it possible to up priority of this feature request?

akhozov avatar Sep 26 '23 12:09 akhozov

This ticket is nearly 3 years old and a very basic feature. I wouldn't bet they will fix this anytime soon.

Tragen avatar Sep 26 '23 13:09 Tragen

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Dec 19 '23 17:12 ghost

yep for sure would like this feature. Looking to be able to have locale codes embedded into urls as way to support direct links for desired languages etc.

domain.com/{locale?}/myPage domain.com/myPage
domain.com/en-US/myPage domain.com/es-US/myPage

StephenWBertrand avatar Feb 09 '24 16:02 StephenWBertrand

I just spent a few hours trying to figure out why my constraint doesn't work... at least now I know.

But please, support such a basic feature. I am very suprised it is not supported for blazor.

ax-meyer avatar Mar 20 '24 16:03 ax-meyer