Blazor Layout conflict with MainLayout

480 Views Asked by At

So i want a specific page to use another layout and rules but the code initialize of the mainLayout is executed event if the page doesn't use or inherit of it.

PageWithOtherLayout.razor

@layout NoMenuLayout
@page "/pagewithotherlayout"

App.razor

<CascadingAuthenticationState>
    <Router AppAssembly="@typeof(App).Assembly">
        <Found Context="routeData">
            <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
            <FocusOnNavigate RouteData="@routeData" Selector="h1" />
        </Found>
...

MainLayout.razor

    protected async override Task OnInitializedAsync()
    {
        base.OnInitialized();
        var user = (await AuthStat).User;
        isAuth = user.Identity.IsAuthenticated;
        if (!isAuth)
        {
           NavigationManager.NavigateTo("./login");
        }
    }

I didn't find anything usefull but is seem to come from the routing process of blazor.

1

There are 1 best solutions below

3
clamchoda On

Somewhere in your application you are loading MainLayout.razor. If you set the debug launch URL directly to pagewithotherlayout you will see that the OnInitializedAsync is not called in MainLayout.razor.

I just tested this on an empty layout.

NoMenuLayout.razor

@inherits LayoutComponentBase

<div class="main">    
    <div class="content px-4">
        @Body
    </div>
</div>