Layout Pages: A Shared Page Shell
A layout page, conventionally named _Layout.cshtml and stored in Views/Shared, defines the common HTML shell (head, navigation, footer) that wraps every individual view's content. A view opts into a layout either by setting Layout = "~/Views/Shared/_Layout.cshtml" in its own @{ } block, or more commonly by leaving that unset and letting Views/_ViewStart.cshtml apply a default layout to every view in the application, avoiding repetition across dozens of views.
Cricket analogy: Like every match at a given stadium sharing the same pitch, boundary rope, and sightscreen setup regardless of which two teams are playing, _Layout.cshtml provides the shared shell that every individual view's content plays out within.
RenderBody, RenderSection, and _ViewStart
Inside _Layout.cshtml, @RenderBody() marks the single spot where the calling view's main content is injected; every layout must call it exactly once. @RenderSection("Scripts", required: false) defines an optional named slot that individual views can fill using @section Scripts { ... }, commonly used for page-specific JavaScript placed at the bottom of the layout for performance. _ViewStart.cshtml runs before every view in its directory (and subdirectories) and typically just sets Layout = "~/Views/Shared/_Layout.cshtml", centralizing that assignment instead of repeating it in every view.
Cricket analogy: Like a designated 'drinks break' slot in a Test match schedule that's mandatory once per session, while an optional extra strategy huddle can be called only if a captain requests it, RenderBody is the mandatory slot and RenderSection is the optional one.
<!-- Views/Shared/_Layout.cshtml -->
<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title - MyStore</title>
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@Html.Partial("_NavBar")
<div class="container">
@RenderBody()
</div>
@Html.Partial("_Footer")
<script src="~/Scripts/jquery.js"></script>
@RenderSection("Scripts", required: false)
</body>
</html>
<!-- Views/_ViewStart.cshtml -->
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}Partial Views: Reusable Fragments
A partial view is a smaller .cshtml file, typically prefixed with an underscore like _ProductCard.cshtml, rendered inline via @Html.Partial("_ProductCard", product) or the async Html.RenderPartial / Html.PartialAsync variants. Unlike a layout, a partial view doesn't wrap other content, it is content, and it's ideal for markup repeated across multiple views, such as a navigation bar, a shopping-cart mini-summary, or a single row in a table that both the main list view and an AJAX-refreshed update reuse identically.
Cricket analogy: Like a broadcaster reusing the same 'player stats' graphic template across every innings without redesigning it each time, a partial view like _PlayerStats.cshtml is rendered wherever needed instead of duplicating that markup in every view.
Html.RenderPartial() writes directly to the response stream and returns void, so it must be called inside an explicit code block like @{ Html.RenderPartial("_Footer"); } rather than with the @ expression syntax; using @Html.RenderPartial(...) will not compile. Html.Partial(), by contrast, returns an MvcHtmlString and can be used with @ directly.
Choosing Between Partial Views, Layouts, and Sections
Use a layout when you need a whole-page wrapper applied consistently, such as the header/footer shell every page shares. Use a partial view when you need a reusable content fragment that multiple, otherwise-different views embed, such as a card component reused in a grid and a detail page. Use @section blocks when a specific view needs to inject content into a specific, named slot the layout defines, such as page-specific meta tags or a script bundle that shouldn't load on every page.
Cricket analogy: Like choosing between the fixed stadium structure (layout), a reusable graphic overlay used across multiple broadcasts (partial view), and an optional special commentary segment only aired for a specific match (section), each MVC tool matches a different scope of reuse.
- _Layout.cshtml defines the shared page shell; @RenderBody() marks the single required slot for the calling view's content.
- _ViewStart.cshtml centralizes the Layout = "..." assignment so every view doesn't need to repeat it.
- @RenderSection("Name", required: false) defines an optional named slot filled per view with @section Name { ... }.
- Partial views (e.g. _ProductCard.cshtml) are reusable content fragments, not page wrappers, rendered via Html.Partial().
- Html.RenderPartial() writes directly to the output and must be called in a code block, not with @ expression syntax.
- Choose layouts for whole-page shells, partial views for reused fragments, and sections for optional named layout slots.
Practice what you learned
1. What must every _Layout.cshtml file call exactly once?
2. What is the purpose of _ViewStart.cshtml?
3. How does a view supply content for an optional layout section like Scripts?
4. Why must Html.RenderPartial() be called inside a @{ } code block instead of with @ expression syntax?
5. When should you use a partial view instead of a layout?
Was this page helpful?
You May Also Like
The Razor View Engine
Understand how Razor compiles .cshtml templates into C# classes, its @ syntax, encoding rules, and how it fits into the ASP.NET MVC request pipeline.
HTML Helpers
Learn how HtmlHelper extension methods generate form controls, links, and validation markup in Razor views, and how they differ from raw HTML.
Strongly Typed Views
Learn how the @model directive binds a Razor view to a specific C# class, enabling IntelliSense, compile-time checking, and clean use of lambda-based helpers.
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