100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
.NET Framework

Layout Pages in Web Pages

How shared layout pages, the RenderBody and RenderSection methods, and reusable partial views let ASP.NET Web Pages sites keep a consistent header, footer, and navigation across many content pages.

Web Pages FoundationsIntermediate9 min readJul 10, 2026
Analogies

Why Layout Pages Exist

A typical site needs the same header, navigation bar, and footer on every page, and repeating that markup in every .cshtml file would be both tedious and error-prone whenever a navigation link changes. A layout page solves this by defining the shared HTML skeleton once, conventionally named _SiteLayout.cshtml with a leading underscore to mark it as a helper file that Razor won't serve directly as its own URL, and every content page opts into that skeleton by setting the Layout property at the top of the file and calling RenderBody() to indicate exactly where its own content should be inserted.

🏏

Cricket analogy: A layout page is like a stadium's fixed structure, the same stands, sightscreens, and boundary rope used for every single match played there, while each individual game supplies its own unique action within that unchanging venue.

Setting Up a Layout

html
<!-- Shared/_SiteLayout.cshtml -->
<!DOCTYPE html>
<html>
<head>
    <title>@Page.Title - My Site</title>
    <link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
    <header>
        <nav><a href="~/">Home</a> | <a href="~/About">About</a></nav>
    </header>
    <main>
        @RenderBody()
    </main>
    <aside>
        @RenderSection("Sidebar", required: false)
    </aside>
    <footer><p>&copy; 2026 My Site</p></footer>
</body>
</html>

<!-- About.cshtml -->
@{
    Layout = "~/Shared/_SiteLayout.cshtml";
    Page.Title = "About Us";
}
<h1>About Us</h1>
<p>We build things with ASP.NET Web Pages.</p>

@section Sidebar {
    <p>Founded in 2019.</p>
}

Setting Layout to a path pointing at the shared file tells Razor to wrap the current page's output inside that layout's markup at the point where @RenderBody() appears, and any content in the page itself outside of a named @section block becomes exactly what RenderBody() outputs. RenderSection works alongside RenderBody for optional named regions, like a sidebar or a page-specific script block, that not every content page needs to fill in; passing required: false to RenderSection tells the layout it's fine if a given page never defines that section, whereas omitting that argument, or setting it true, throws an exception at runtime for any page missing that section.

🏏

Cricket analogy: RenderBody is like the main square where the day's play actually happens inside a fixed stadium, while RenderSection resembles an optional post-match presentation ceremony that only occurs on days when there's a result to celebrate.

Files and folders prefixed with an underscore, such as _SiteLayout.cshtml and files under a folder named _partials, are treated by ASP.NET Web Pages as private and cannot be requested directly by URL, only referenced from other Razor files.

Reusable Partial Pages

Beyond full-page layouts, Web Pages supports smaller reusable fragments through the RenderPage() method, which lets you pull the output of another .cshtml file into the current page at an arbitrary point, useful for something like a repeated product-card widget or a shared search box that appears in multiple places that aren't necessarily the same across the whole site. Unlike Layout, which wraps the calling page, RenderPage() is called from within the page's own content and can even pass data to the partial via an optional second parameter, an object array accessible inside the partial through the PageData collection.

🏏

Cricket analogy: RenderPage is like inserting a specific fielding drill clip into a training video at just the point a coach wants to demonstrate it, a reusable segment pulled in wherever it's relevant, not tied to the whole session's structure.

RenderPage() and Layout serve different roles: Layout wraps the current page inside a shared outer template, while RenderPage() pulls a smaller reusable fragment into a specific spot within the page's own content. Confusing the two can lead to duplicated headers or missing content.

  • A layout page defines shared markup, like header/nav/footer, once and content pages opt in via the Layout property.
  • RenderBody() marks where a content page's own markup is inserted into the layout.
  • RenderSection lets a layout define optional named regions, with required:false making them non-mandatory.
  • Files prefixed with an underscore, like _SiteLayout.cshtml, are private and never directly reachable by URL.
  • RenderPage() pulls in a smaller reusable fragment at a specific point within a page's own content.
  • RenderPage() can pass data to the partial via an object array accessed through PageData.
  • Layout wraps the whole page; RenderPage() inserts a fragment at one spot — they solve different reuse problems.

Practice what you learned

Was this page helpful?

Topics covered

#NETFramework#ClassicASPNETWebFormsWebPagesStudyNotes#MicrosoftTechnologies#LayoutPagesInWebPages#Layout#Pages#Web#Exist#WebDevelopment#StudyNotes#SkillVeris