Understanding shared layouts
Working with Layouts
Change views and layout pages
Try selecting the menu links MvcMovie, Home, and Privacy. Each page shows the same menu layout. The menu layout is implemented in the Views/Shared/_Layout.cshtml
file.
Open the Views/Shared/_Layout.cshtml
file.
Layout templates allow:
Specifying the HTML container layout of a site in one place.
Applying the HTML container layout across multiple pages in the site.
Find the @RenderBody()
line. RenderBody
is a placeholder where all the view-specific pages you create show up, wrapped in the layout page. For example, if you select the Privacy link, the Views/Home/Privacy.cshtml
view is rendered inside the RenderBody
method.
Change the title, footer, and menu link in the layout file
Replace the content of the Views/Shared/_Layout.cshtml
file with the following markup.
Note: The changes are summarized after the code snippet
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - Movie App</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
</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-fluid">
<a class="navbar-brand" asp-area="" asp-controller="Movies" asp-action="Index">Movie App</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-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 justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="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">
© 2023 - Movie App - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
@await RenderSectionAsync("Scripts", required: false)
</body>
</html>
The preceding markup made the following changes:
Three occurrences of
MvcMovie
toMovie App
.The anchor element
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">MvcMovie</a>
to<a class="navbar-brand" asp-controller="Movies" asp-action="Index">Movie App</a>
.
In the preceding markup, the
asp-area=""
anchor Tag Helper attribute and attribute value was omitted because this app isn't using Areas.
Save the changes and select the Privacy link. Notice how the title on the browser tab displays Privacy Policy - Movie App instead of Privacy Policy - MvcMovie
Select the Home link.
Notice that the title and anchor text display Movie App. The changes were made once in the layout template and all pages on the site reflect the new link text and new title.
How is the shared layout applied to all pages?
Examine the Views/_ViewStart.cshtml
file:
@{
Layout = "_Layout";
}
The Views/_ViewStart.cshtml
file brings in the Views/Shared/_Layout.cshtml
file to each view. The Layout
property can be used to set a different layout view, or set it to null
so no layout file will be used.
Last updated