Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 41 ยท Routing and Layouts

Layouts and Nested Layouts

Build shared page structure in Blazor with LayoutComponentBase, @Body, the @layout directive, _Imports defaults, nested layouts and sections that pages can fill.

Almost every site repeats the same frame around its pages: a header, a navigation menu, a footer. Copying that markup into every page would be a maintenance nightmare, so Blazor offers layouts. A layout is a component that wraps a page: it renders the shared frame and drops the page's content into a marked spot.

You will learn how a layout is written, how a page picks one, how to set a default for a whole folder, how layouts nest (an admin area inside the main site), and how a page can push small pieces of content, such as a title, into the layout using sections. The framework pieces are shown as read-only Razor; the runnable stand-ins model the wrapping idea with ordinary C# delegates.

Writing a layout

A layout inherits from LayoutComponentBase, which gives it a Body property of type RenderFragment. Render @Body where the page should appear.

RAZOR
@inherits LayoutComponentBase

<header>
    <h1>My Shop</h1>
    <NavMenu />
</header>

<main>
    @Body
</main>

<footer>&copy; 2026 My Shop</footer>

In the template that Visual Studio and dotnet new blazor create, this file is Components/Layout/MainLayout.razor. It is registered as the default layout in the Routes.razor file through the RouteView:

RAZOR
<Router AppAssembly="typeof(Program).Assembly">
    <Found Context="routeData">
        <RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" />
        <FocusOnNavigate RouteData="routeData" Selector="h1" />
    </Found>
</Router>

DefaultLayout applies to every routable component that does not choose its own.

Choosing a layout per page

Use @layout in a page:

RAZOR
@page "/login"
@layout CenteredLayout

<h1>Sign in</h1>

To give a whole folder the same layout, put the directive in that folder's _Imports.razor. Every component below it inherits the directive, and a single page can still override it. @layout accepts any component type that derives from LayoutComponentBase. If you want a page with no frame at all, write a layout that only renders @Body.

How wrapping works

Rendering a page with a layout is like putting a function inside a function. The layout's template runs, and where it hits @Body it runs the page's template. The stand-in below models a RenderFragment as Func<string>; a layout is a function that takes the body fragment and returns a bigger fragment.

C# Stand-in layouts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Because the page is just a value passed in, one layout serves any number of pages, and the header markup exists exactly once.

Nested layouts

A layout can itself have a layout. Suppose the admin area needs a side menu inside the normal frame. Create AdminLayout that inherits LayoutComponentBase and declares @layout MainLayout. The page is wrapped by AdminLayout, and the result is wrapped by MainLayout.

RAZOR
@inherits LayoutComponentBase
@layout MainLayout

<div class="admin">
    <aside><a href="admin/users">Users</a> <a href="admin/orders">Orders</a></aside>
    <section>@Body</section>
</div>
flowchart TB
    M["MainLayout<br/>header and footer"] --> A["AdminLayout<br/>side menu"]
    A --> P["Page: /admin/users"]

The page is innermost; layouts apply from the inside out.

The same composition in plain C#:

C# Stand-in nested layouts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The order matters: the first layout in the chain wraps the page, the last is the outermost frame.

Sections: sending content up to the layout

A page often wants to put something in the header: a title, a toolbar, a button. Since .NET 8 the SectionOutlet and SectionContent components do this. The layout declares a named outlet, and any component below can fill it.

RAZOR
@* In MainLayout.razor *@
<header>
    <SectionOutlet SectionName="page-title" />
</header>
RAZOR
@* In any page *@
<SectionContent SectionName="page-title">
    <h1>Order history</h1>
</SectionContent>

The outlet shows the content that the most recently rendered SectionContent with that name provided. Use sections when the layout owns the place but the page owns the text. For simple cases, PageTitle and HeadContent do the same job for the document <title> and <head>.

RAZOR
<PageTitle>Order history - My Shop</PageTitle>

Every page should have a unique PageTitle: it improves search results and accessibility. Our next stand-in shows the idea behind sections as a dictionary of named slots.

C# Stand-in sections

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Layout design tips

  • Keep layouts thin: structure only. Logic belongs in child components such as NavMenu.
  • A layout is not re-created when you navigate between two pages that use it. Its state (an open menu, a scroll position) survives, which is useful but means data loaded in a layout does not refresh on navigation. Use LocationChanged or a service if it should.
  • A layout can have its own @code, inject services and show things like the signed-in user's name.
  • Parameters cannot be passed to a layout from a page. Share data using a cascading value or a service, covered in Component State and Cascading Values.
  • A layout can inherit another class, but it must ultimately derive from LayoutComponentBase.

The @Body property is only set when the layout is rendered by the router. Rendering a layout directly with <MainLayout /> shows nothing in its body slot.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where does a page's content appear inside a layout?

    LayoutComponentBase exposes the page as the Body render fragment, which the layout renders with @Body.
  2. 2. How do you apply one layout to every page in a folder?

    Directives in _Imports.razor apply to all components below that folder, and a page can still override them.
  3. 3. AdminLayout declares @layout MainLayout. What is the render order for an admin page?

    The page is innermost, wrapped by AdminLayout, which is itself wrapped by MainLayout.
  4. 4. What does SectionContent do?

    A page fills a named outlet declared by the layout, so the layout owns the place and the page owns the content.

Summary

  • A layout inherits LayoutComponentBase and renders the page with @Body.
  • DefaultLayout on RouteView sets the app-wide frame; @layout overrides it per page or per folder through _Imports.razor.
  • Layouts nest: the page is wrapped by its layout, which is wrapped by that layout's own layout.
  • SectionOutlet and SectionContent let a page send small pieces up to the layout; PageTitle sets the document title.
  • Layouts persist across navigations between their pages.

Next: Query Strings and URL State