Layouts

By Syncfusion®, Inc. · Last updated

Shared page frames in Blazor: LayoutComponentBase and @Body, the default layout, nested layouts, LayoutView, the NavMenu, sections with SectionOutlet and SectionContent, and the layout's error UI.

What you will learn

  • Write a layout with LayoutComponentBase and @Body
  • Set the main layout with DefaultLayout in Routes.razor
  • Nest layouts and use LayoutView
  • Fill a layout spot with SectionOutlet and SectionContent

Layouts Overview

  • In Project Structure, you saw MainLayout.razor in the Components/Layout folder.
  • A layout is the shared frame around your pages: the header, the menu and the footer.
  • Each page shows its own content inside that frame.
  • In this lesson, you will make layouts and choose which page uses which layout.

Real-World Example:

  • Every page of Ravi's app shows the shop name at the top, a menu on the left and the opening hours at the bottom.
  • Ravi puts these in one layout.
  • The Menu, Order and Contact pages only contain their own content.

Animation: a layout with a header, a side menu and a footer stays the same. In the middle, where @Body is, the page content changes from Menu to Order to Contact.

☕ Ravi's Tea Shop (layout)

Menu Order Contact

@Body

Menu page

Order page Contact page

Open 7 AM to 9 PM (layout)

The layout stays. Only the page inside @Body changes.

Layout Components (LayoutComponentBase)

First, one word you need:

  • Inherit = take everything from another class, so you do not write it again.

Now the layout:

  • A layout is a normal .razor file with one extra line at the top: @inherits LayoutComponentBase.
  • This line turns the component into a layout.

Rendering Page Content (@Body)

  • @Body marks where the page goes inside the layout.
  • Without @Body, you see only the frame. The page is missing.

In a real app, the Router puts each page inside its layout. In the code boxes, LayoutView does that job. You learn it later in this lesson.

In the below example, ShopLayout.razor has a header and a footer, with @Body between them. Home.razor shows "Today's special" inside it. Press ▶ Run.

  • Delete @Body in ShopLayout.razor and press ▶ Run. The header and footer stay, but the page content disappears.

Default Layout (RouteView DefaultLayout)

  • The app's main layout is set once, in Routes.razor.
  • DefaultLayout="typeof(Layout.MainLayout)" means every page uses MainLayout.

Nested Layouts

  • A nested layout is a layout that is placed inside another layout.
  • The inner layout uses @layout to connect itself to the outer layout.
  • When a page uses the inner layout, it automatically gets both layouts.

In this example, MenuLayout.razor adds a sidebar and uses MainShell as its parent layout. So, a page that uses MenuLayout gets both the sidebar from MenuLayout and the shared page structure from MainShell. This is called a nested layout.

LayoutView Component

  • LayoutView displays content inside a layout without using the Router.
  • It is useful for showing content that is not a page, such as a "Not Found" message.

In the example below, Home.razor uses LayoutView to display content inside either ShopLayout or KioskLayout, and the buttons switch between the two layouts.

LayoutView ignores any @layout line inside the content. For normal pages, use @layout.

Navigation Menu in a Layout (NavMenu)

  • In Built-in Components, you made menu links with NavLink.
  • The template keeps those links in NavMenu.razor and shows <NavMenu /> in MainLayout.
  • So the menu appears on every page.

Sections

  • A section allows a page to place content in a specific area of a layout, such as a header or toolbar.
  • The layout defines where the content should appear using SectionOutlet.
  • The page provides the content for that location using SectionContent.

In the example below, ShopLayout.razor creates a location named "top-bar" using SectionOutlet. Home.razor fills that location with an Order now button using SectionContent, so the button appears in the header of the layout.

Layout Error UI

  • MainLayout has a hidden yellow bar. Blazor shows it when an error is not caught.
  • In Built-in Components, you used ErrorBoundary. Put it around @Body so one broken page does not break the whole frame.

In the below example, SafeLayout.razor wraps @Body in an ErrorBoundary. Click the button: the page shows an error message, but the header still works.

In the template, the error bar's style lives in MainLayout.razor.css under #blazor-error-ui. Change it there to match your app's colours.

Common mistakes

MistakeWrong ideaCorrect ideaWhat goes wrong
Forgetting @BodyThe layout adds the page by itselfPut @Body where the page goesOnly the header and footer show
A layout without @inherits LayoutComponentBaseAny component can be a layoutAdd @inherits LayoutComponentBaseAn error: Body does not exist
Expecting LayoutView to read the page's @layoutIt works like the RouterIt uses only its Layout valueThe wrong layout shows
Different names in SectionOutlet and SectionContentAny name connects themUse the same SectionNameThe content never appears

Check Yourself

Exercises

What to Remember

  • A layout inherits LayoutComponentBase and shows the page at @Body.
  • DefaultLayout in Routes.razor sets the main layout for every page.
  • A layout with its own @layout is nested.
  • LayoutView shows content in a layout without the Router.
  • Put NavMenu in the layout, so every page has the menu.
  • SectionOutlet and SectionContent let a page fill a spot in the layout.
  • #blazor-error-ui shows uncaught errors. Wrap @Body in ErrorBoundary to keep the frame working.

Next: Forms & Events