Search code examples
asp.net-coreblazorblazor-server-siderazor-components

How to navigate to a folder's default page (.razor) in Blazor Server app?


In my blazor app I have a hierarchy of razor component (pages) folders. Each folder represents a data model and includes 4 .razor pages.

Contacts
     List.razor
     View.razor
     Edit.razor
     Create.razor
Accounts
     List.razor
     View.razor
     Edit.razor
     Create.razor

etc.

URLs for these pages:

/contacts/list
/contacts/view
/contacts/edit
/contacts/create
/accounts/list
/accounts/view
/accounts/edit
/accounts/create

My goal is when I type in browser /contacts it should be routed to a default page in that folder, which is /contacts/list Or if I type /accounts, it must route to /accounts/list

How can I achieve that?


Solution

  • You can add a second @page directive on the page you want to be the canonical top-level.

    @page "/contacts/list"
    @page "/contacts"
    

    If you absolutely must redirect from /contacts to /contacts/list you can use NavigationManager on a /contacts page to redirect to the list page.