MADJOE
MADJOE

Reputation: 269

Blazor WASM Page Routing

Is there a way to define the page route of a razor component through a variable? One of the reason why I want it to be done this way is that whenever I need to define a redirect link on another page, I can just use the variable rather than hard coding it again.

By default, when we want to define a razor component as a page we do it like

@page "/product/{id}"

I want it to be centralized in a sense, that it would look something like this

public class PageRoute
{
    public const string ProductInfo = "/product/{id}";
}

and then I will be calling it on the razor component as

@page PageRoute.ProductInfo

Upvotes: 2

Views: 536

Answers (1)

MrC aka Shaun Curtis
MrC aka Shaun Curtis

Reputation: 30375

I want it to be centralized in a sense, that it would look something like this ....

No, not with out of the box Blazor. @page = "/" gets pre-compiled into an attribute :

[Microsoft.AspNetCore.Components.RouteAttribute("/")]
public partial class Index : Microsoft.AspNetCore.Components.ComponentBase {
....
}

Which is fixed at compile time. If you want a dynamic router you'll need to write your own router. Search the Interent for "Blazor Dynamic Routing" and "Blazor Custom Routing" for more information.

Upvotes: 2

Related Questions