ASP.NET Core MVC Layouts

This tutorial covers how to use layout pages in ASP.NET Core MVC applications to define a common HTML structure for multiple views.

What are Layout Pages?

Layout pages provide a way to define a consistent look and feel across your web application. Instead of repeating common HTML elements like headers, footers, and navigation menus in every view, you can define them once in a layout page and have your individual views render their specific content within that layout.

Creating a Layout Page

By default, ASP.NET Core MVC applications created with the default template include a layout page located at Views/Shared/_Layout.cshtml. If you don't have one or want to create a new one, you can add a new HTML file to the Views/Shared folder.

Example: Views/Shared/_Layout.cshtml

This is a simplified example of a typical layout file:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - MyApp</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
    <header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
            <div class="container">
                <a class="navbar-brand" asp-area="" asp-page="/Index">MyApp</a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
                    <ul class="navbar-nav flex-grow-1">
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </header>
    <div class="container">
        <main role="main" class="pb-3">
            @RenderBody()
        </main>
    </div>

    <footer class="border-top footer text-muted">
        <div class="container">
            &copy; 2023 - MyApp - <a asp-area="" asp-page="/Privacy">Privacy</a>
        </div>
    </footer>
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>
    @RenderSection("Scripts", required: false)
</body>
</html>

Understanding the Key Elements

  • @RenderBody(): This is a special directive that acts as a placeholder. When a view renders, its HTML content will be inserted here.
  • @ViewData["Title"]: This allows you to set the title of the page dynamically from individual views.
  • @RenderSection("Scripts", required: false): This directive allows views to inject JavaScript code specifically for that view, typically placed at the end of the body. The required: false parameter means that views are not obligated to provide content for this section.
  • ~/... syntax: This indicates a path relative to the web root of your application.

Specifying a Layout for a View

By default, views in the Views/ folder will automatically use the _Layout.cshtml file in the Views/Shared/ folder. You can also explicitly specify a layout for a view by setting the Layout property in the view itself:

Example: Specifying Layout in a View

@{
    ViewData["Title"] = "Home Page";
    Layout = "~/Views/Shared/_Layout.cshtml"; // Explicitly set the layout
}

<h1>Welcome!</h1>
<p>This content will be rendered within the layout page.</p>

@section Scripts {
    <script>
        console.log("This script is specific to the Home page.");
    </script>
}

Using Different Layouts

You can create multiple layout files (e.g., _AdminLayout.cshtml, _PublicLayout.cshtml) and assign them to different views as needed.

Layouts in Razor Pages

Razor Pages also utilize layout pages in a similar fashion, typically using _Layout.cshtml by default. The concept is identical: provide a common structure and use @RenderBody() to inject the page's content.

Understanding and effectively using layout pages is fundamental to building maintainable and consistent ASP.NET Core MVC applications.