Skip to content

Blazor WASM: Programmatically set layout for a page or component #26122

@zachion

Description

@zachion

We have a Blazor WASM project with two layouts a) LoginLayout, b) MainLayout. On the LoginLayout we display a minimum set of components (i.e. login component) for code simplification purposes. The LoginLayout is intended for not authorized users and the MainLayout is used for authenticated users instead. We also have areas where both authorized and not authorized users have access, such as terms of use and privacy policy. Currently we are setting the layouts for the unrestricted areas using one of the two layouts (LoginLayout) but ideally we would need different layouts depending on the authentication state for each user. The only way to achieve this we've found is have multiple pages / components and on different paths so we can explicitly set them to use our two different layouts. But this created duplicated code and we had to remove to keep our codebase clean and we just manually defining layouts on the top of each page instead and by picking only one of the two available layouts.

Describe the solution you'd like

Ideally we would need the ability to specify layouts for each component or page programmatically, like the following example:

if (condition)
        {
            @layout MainLayout
        }
        else
        {
            @layout LoginLayout
        }

or

<AuthorizeView>
    <Authorized>
        @layout MainLayout
    </Authorized>
    <NotAuthorized>
          @layout LoginLayout
    </NotAuthorized>
</AuthorizeView>

Additional context

Currently if we try this we get syntax highlight error and we are unable to do so:
image
image

Project does not build either:
image

Metadata

Metadata

Labels

✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Status: Resolvedarea-blazorIncludes: Blazor, Razor Componentsfeature-blazor-wasmThis issue is related to and / or impacts Blazor WebAssembly

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions