Is it possible to use @layout with serverside Blazor components? #25861
-
I would like to have a cshtml page that loads a serverside blazor component with Html.RenderComponentAsync. I would like that component to contain another blazor component that is based on a @layout. Everything works except that the component that has a @layout is just rendered by itself without its layout. Am I trying to do something unsupported? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You can do this but you'll need to use the For example, if your <LayoutView Layout="@typeof(MainLayout)">
<SomeOtherComponent />
</LayoutView> ... then this will render Alternatively inside |
Beta Was this translation helpful? Give feedback.
You can do this but you'll need to use the
<LayoutView>
component (or a<Router>
), because that's what causes the Blazor component's layout to be rendered.For example, if your
.cshtml
file rendersMyHost.razor
, containing:... then this will render
SomeOtherComponent
inside theMainLayout
layout. Using this approach you don't even need to put a@layout
attribute on your page.Alternatively inside
MyHost.razor
you could render a Blazor<Router>
component. This would then take case of picking which other component to render based on the URL and your@page
attributes, plus it automatically picks whichever lay…