Lesson 12 of 40 Views Beginner 30 min

Layout Pages and Shared UI

Learn how layout pages help you build a consistent user interface in ASP.NET Core MVC. Instead of repeating the same header, navigation menu, and footer on every page, you can define them once in a shared layout and reuse them across your application.

Part 1: What a Layout Page Does

A layout page acts like a master template for your ASP.NET Core application. It defines the outer structure of the page, while individual views provide the content that appears inside that structure.

In most MVC applications, the layout contains common interface elements such as the site header, navigation bar, footer, stylesheet links, and script references.

This helps ensure that every page in the application has a consistent design and saves you from repeating the same HTML in multiple files.

Part 2: The Default Layout File

In an ASP.NET Core MVC project, the main layout file is usually stored in:

Views/Shared/_Layout.cshtml

This file contains the shared page structure for the whole site. When a view is rendered, ASP.NET Core inserts the view content into this layout.

Part 3: Basic Layout Example

A simple layout file may look like this:

<!DOCTYPE html>
<html>
<head>
  <title>My ASP.NET Core App</title>
</head>
<body>
  <header>
    <h1>My Website</h1>
  </header>

  <main>
    @RenderBody()
  </main>

  <footer>
    <p>Copyright 2026</p>
  </footer>
</body>
</html>

The most important part is @RenderBody(). This is the placeholder where the content of the current view will appear.

Part 4: Why Layout Pages Are Useful

Without layout pages, every view would need to repeat the same HTML for the header, navigation menu, footer, and shared scripts. That makes the application harder to maintain.

Without Layout With Layout
Repeated header and footer code on every page Shared structure written once
Harder to update site-wide design Easier to update all pages at once
More duplication Cleaner and more maintainable project

Part 5: How Views Use a Layout

In ASP.NET Core, views usually use a layout automatically through the _ViewStart.cshtml file.

@{
  Layout = "_Layout";
}

This tells the framework that each view should be rendered inside the shared layout. As a result, individual views only need to focus on their own content.

Part 6: Shared UI Elements

Layout pages are ideal for user interface elements that should appear throughout the site. Common examples include:

This creates a consistent experience for users and keeps your project more organized.

Part 7: Optional Sections in Layouts

A layout can also define optional sections that individual views may fill in. One common example is a Scripts section for page-specific JavaScript.

@RenderSection("Scripts", required: false)

Then inside a specific view:

@section Scripts {
  <script>
    console.log("Page-specific script");
  </script>
}

This gives you flexibility while still keeping the main layout clean.

Part 8: Best Practices

A well-designed layout improves maintainability and gives your ASP.NET Core project a more professional structure.

Summary

Layout pages are one of the most useful features of ASP.NET Core MVC. They allow you to define a shared structure for your site and avoid repeating common interface code. Once you understand layouts, it becomes much easier to build clean, consistent, and scalable applications.

VISUAL STUDIO 2026 MADE EASY
Recommended Book

VISUAL STUDIO 2026 MADE EASY

Build real applications with C#, VB.NET, Python, JavaScript, C++, and .NET 10. A practical companion for mastering Visual Studio 2026 step by step.