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.razorin theComponents/Layoutfolder. - 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
.razorfile with one extra line at the top:@inherits LayoutComponentBase. - This line turns the component into a layout.
Rendering Page Content (@Body)
@Bodymarks 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
@BodyinShopLayout.razorand 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 usesMainLayout.
Nested Layouts
- A nested layout is a layout that is placed inside another layout.
- The inner layout uses
@layoutto 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
LayoutViewdisplays 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.razorand shows<NavMenu />inMainLayout. - 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
MainLayouthas a hidden yellow bar. Blazor shows it when an error is not caught.- In Built-in Components, you used
ErrorBoundary. Put it around@Bodyso 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
| Mistake | Wrong idea | Correct idea | What goes wrong |
|---|---|---|---|
Forgetting @Body | The layout adds the page by itself | Put @Body where the page goes | Only the header and footer show |
A layout without @inherits LayoutComponentBase | Any component can be a layout | Add @inherits LayoutComponentBase | An error: Body does not exist |
Expecting LayoutView to read the page's @layout | It works like the Router | It uses only its Layout value | The wrong layout shows |
Different names in SectionOutlet and SectionContent | Any name connects them | Use the same SectionName | The content never appears |
Check Yourself
Exercises
What to Remember
- A layout inherits
LayoutComponentBaseand shows the page at@Body. DefaultLayoutinRoutes.razorsets the main layout for every page.- A layout with its own
@layoutis nested. LayoutViewshows content in a layout without the Router.- Put
NavMenuin the layout, so every page has the menu. SectionOutletandSectionContentlet a page fill a spot in the layout.#blazor-error-uishows uncaught errors. Wrap@BodyinErrorBoundaryto keep the frame working.
Next: Forms & Events