Blazor routing with regex

207 views Asked by At

I am new to Blazor and looking what are the possibilities with this. I would like to migrate my asp.net website to blazor. I actually have urls like this :

www.mywebsite.fr/product/a-12
www.mywebsite.fr/product/details/a-12
www.mywebsite.fr/toto/tata/titi/b-9

But in the following documentation url, it seems that Blazor doesn't manage urls with regex. https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/routing?view=aspnetcore-7.0

I tried the following directive :

@page "/product/{Id:regex(^a\-\d{1,2}$)}"

but without success :(

Am I wrong ? If not, how can I do ?

1

There are 1 answers

2
titol On

You can catch all routes and handle it at component level:

@page "/product/{*pageRoute}"

@code {
    [Parameter]
    public string? PageRoute { get; set; }
}

After having PageRoute you can then decide, based on regex, to navigate user away, or process with request. In case above, if your route be product/123/456/a-2, PageRoute paramater will be 123/456/a-2.