Route to a particular page based on a Cascading Value. (Similar to how AuthorizeRouteView works)

Jeremy Champion 0 Reputation points
2023-03-21T20:32:28.0733333+00:00

In the following code example, I am using the AuthorizeRouteView to send users to the LoginRedirect page if they are not authenticated.

If they are authenticated, I would then like to route them based on a value in my CascadingAppState.

I've tried writing a custom route view (which failed) and I've tried hooking the NavigationManager LocationChanged event and checking to see if I need to redirect there.

Neither worked (at least with my implementation).

Any help would be much appreciated!

<CascadingAuthenticationState>
    <CascadingAppState>
        <Router AppAssembly="@typeof(App).Assembly">
            <Found Context="routeData">
                <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
                    <NotAuthorized>
                        <LoginRedirect />
                    </NotAuthorized>
                </AuthorizeRouteView>
                <FocusOnNavigate RouteData="@routeData" Selector="h1" />
            </Found>
            <NotFound>
                <PageTitle>Not found</PageTitle>
                <LayoutView Layout="@typeof(MainLayout)">
                    <p role="alert">Sorry, there's nothing at this address.</p>
                </LayoutView>
            </NotFound>
        </Router>
    </CascadingAppState>
</CascadingAuthenticationState>
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,402 questions
{count} votes