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
<!-- 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>© 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
1. What is the conventional naming pattern for a shared layout file in ASP.NET Web Pages?
2. What does RenderBody() do inside a layout page?
3. What happens if a layout calls @RenderSection("Sidebar") without required: false, and a content page doesn't define that section?
4. How does RenderPage() differ from setting the Layout property?
5. How can data be passed into a partial page rendered with RenderPage()?
Was this page helpful?
You May Also Like
Razor Syntax in Web Pages
A practical guide to the Razor markup syntax that lets you embed C# or VB.NET code inline with HTML using the @ symbol, including expressions, code blocks, loops, and conditionals.
Helpers in Web Pages
How built-in and custom helpers provide reusable, prebuilt pieces of functionality, from Chart and WebGrid to WebMail and Facebook integration, plus how to write your own helper with @helper.
WebMatrix and Project Structure
How Microsoft's free WebMatrix tool creates and organizes an ASP.NET Web Pages site, including the special App_Data, App_Start, and Account folders and the built-in development web server.
What Is ASP.NET Web Pages?
An introduction to ASP.NET Web Pages, a lightweight framework that mixes server-side C# or VB.NET code directly into HTML using Razor syntax, aimed at simple, file-based site development.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesWindows Batch Scripting Study Notes
Batch · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics