ASP.NET Core MVC Interview Question #16
What is a layout in Razor views?
MVC Fundamentals, Controllers, Views & Routing Junior Beginner
Quick Interview Answer
A layout is a shared Razor template that defines common page structure such as the HTML document, header, navigation, footer, styles, and scripts. Individual views render their page-specific content into the layout through RenderBody.
Detailed Explanation
A layout is a shared Razor template that defines common page structure such as the HTML document, header, navigation, footer, styles, and scripts. Individual views render their page-specific content into the layout through RenderBody.
Layouts reduce duplication and keep a consistent site structure. Views can also define named sections that a layout renders with RenderSection.
Code Example
<!-- Views/Shared/_Layout.cshtml -->
<body>
<header>My Site</header>
<main>
@RenderBody()
</main>
</body>