asp.net-coreblazor

Conditional Layouts based on Parameter


I would like my .razor page to have the Default Layout (MainLayout) unless the LayoutType parameter is == "Tab" I am always getting the TabLayout. The default layout is MainLayout.

@page "/WasteNum/{LayoutType}"

@if (LayoutType == "Tab")
{
    @layout TabLayout 
}

@code {
    [Parameter]
    public string LayoutType { get; set; } = "Main";
}

Solution

  • You could implement logic to use layout dynamically in App.razor 's "Router".Based on your needs you could modify App.razor as following

    @inject NavigationManager _navigationManager
    <Router ...
    ...
            <RouteView RouteData="@routeData" DefaultLayout="GetLayoutType()" />
    ...
    
            <LayoutView Layout="GetLayoutType()">
    ...
    </Router>
    
    
    @code {
    
        private Type GetLayoutType()
        {
            if (_navigationManager.Uri.EndsWith("Tab"))
            {
                return typeof(TabLayout);
            }
            else
            {
                return typeof(MainLayout);
            }
        }
    }