Learn About Layout View In Razor Pages


In this article, we are going to discuss the Layout view in Razor pages. This article can be used by beginners, intermediate, and professionals.
This is the second article on the ASP.NET Core Razor pages series. In the first article, we have created a “Hello World” application using .Net 5 and Razor pages. I recommend reading the first article before the second article. You can find the first article here.
In this article, we are going to cover,
  1. What is Layout View
  2. How to pass title value from Razor pages to the layout view
  3. Static files used on the Layout view
  4. Render Body and Render Section
  5. Tages (asp-area, asp-page) in Razor Layout.
  6. How to add a new menu using Razor Layout view.
The first question that comes to mind is,

What is Layout View?

“Common sections are defined in the layout view like header, footer, menu, etc and then each project-specific content can be plugged in the layout view”.
Suppose we have web applications that have many pages with the same Header, Footer, and Menu, in this case, we should use the Layout view. Each page-specific content can be plugged into the “page – content” section of the below image.
Both project Asp.net Core MVC and Razor pages support layout view. The differences are very minor.
The next question that comes to mind is.

Should we call it Layout Page or Layout View?

@Page directive makes the “.cshtml” file as Razor page. Let’s open the layout view code and see that this tag is available or not.
Layout view is located at : pages/Shared/_layout.cshtml.
Layout view name starts with _ (underscore) which means its shared view.
Double click on _Layout.cshtml file and see that @page is available or not? 
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="utf-8" />  
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0" />  
  6.     <title>@ViewData["Title"] - RazorPages</title>  
  7.     <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />  
  8.     <link rel="stylesheet" href="~/css/site.css" />  
  9. </head>  
  10. <body>  
  11.     <header>  
  12.         <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">  
  13.             <div class="container">  
  14.                 <a class="navbar-brand" asp-area="" asp-page="/Index">RazorPages</a>  
  15.                 <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"  
  16.                         aria-expanded="false" aria-label="Toggle navigation">  
  17.                     <span class="navbar-toggler-icon"></span>  
  18.                 </button>  
  19.                 <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">  
  20.                     <ul class="navbar-nav flex-grow-1">  
  21.                         <li class="nav-item">  
  22.                             <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>  
  23.                         </li>  
  24.                         <li class="nav-item">  
  25.                             <a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>  
  26.                         </li>  
  27.                     </ul>  
  28.                 </div>  
  29.             </div>  
  30.         </nav>  
  31.     </header>  
  32.     <div class="container">  
  33.         <main role="main" class="pb-3">  
  34.             @RenderBody()  
  35.         </main>  
  36.     </div>  
  38.     <footer class="border-top footer text-muted">  
  39.         <div class="container">  
  40.             © 2021 - RazorPages - <a asp-area="" asp-page="/Privacy">Privacy</a>  
  41.         </div>  
  42.     </footer>  
  44.     <script src="~/lib/jquery/dist/jquery.min.js"></script>  
  45.     <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>  
  46.     <script src="~/js/site.js" asp-append-version="true"></script>  
  48.     @await RenderSectionAsync("Scripts", required: false)  
  49. </body>  
  50. </html>  
No, the Layout view does not have @page directive hence technically speaking, we can say it's a Layout view, not a Layout page.

How to pass Title from Razor page to LayoutView?

In the layout view, we have below code,
  1. <head>  
  2.     <meta charset="utf-8" />  
  3.     <meta name="viewport" content="width=device-width, initial-scale=1.0" />  
  4.     <title>@ViewData["Title"] - RazorPages</title>  
  5.     <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />  
  6.     <link rel="stylesheet" href="~/css/site.css" />  
  7. </head>  
Title value set from dynamic property ViewData[“Title”]. Value of viewdata is set on the Razor page. Let’s the open index.cshtml file to prove it.   
  1. @page  
  2. @model IndexModel  
  3. @{  
  4.     ViewData["Title"] = "Home page";  
  5. }  
  7. <div class="text-center">  
  8.     <h1 class="display-4">@Model.Message</h1>  
  9.     <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>  
  10. </div>  
We can see that ViewData[“Title”] value is set in the razor page which is passed in the layout view.

Static Files in Layout

Let us analyzed added static files in Layout view,
  1. <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />  
  2. <link rel="stylesheet" href="~/css/site.css" />  
All static files are store in the lib folder. Asp.net Core MVC and Razor pages are not served static files directly. There are two conditions to server static files.
The first condition is static files should be residing in the wwwroot folder.
The second condition is it should be added to the HTTP middleware pipeline. Click on the startup.cs file and select configure method to see the pipeline.
In the lib folder, we have css files that help us to create a responsive web application.
It also has javascript files added in the layout view.
  1. <script src="~/lib/jquery/dist/jquery.min.js"></script>  
  2. <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>  
  3. <script src="~/js/site.js" asp-append-version="true"></script>  
We can have only one Rendor body section but can have multiple render sections in the layout view.
The Render body is used to inject razor page content in the layout view page.
Render Section()
The Render section is used to inject content in the define section from the razor to the layout view. Multiple sections can be added from the razor page to the layout view. The render section is optional if not required.
Let’s see the below code to understand asp-area and asp-page tags,
  1. <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">  
  2.                    <ul class="navbar-nav flex-grow-1">  
  3.                        <li class="nav-item">  
  4.                            <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>  
  5.                        </li>  
  6.                        <li class="nav-item">  
  7.                            <a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>  
  8.                        </li>  
  9.                    </ul>  
  10.                </div>  
There are many tags available in asp. core. In the above code, we notice two tags,
  1. asp-page
  2. asp-area
We will try to understand these tags,
We all know why the area is used in .Net. To set the area name this attribute is used so routing will work properly.
Please note this attribute available with Razor pages and MVC pages with small changes.
This attribute can be used with razor pages to set anchor href value to a specific page.
Forward slash (“/”) with the name of the page is set to this attribute. In our code, we have set Index page hence code would be,
  1. <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>  

How to add a new Menu?

Right Click on Pages - Add - Razor Pages.

Select Razor Page - Empty and click on add button.
Provide an appropriate name and click on Add button. 
New member empty razor Page added.
We will set the page title and sample message on the member page.
  1. @page  
  2. @model RazorPages.Pages.MemberModel  
  3. @{  
  4.     ViewData["Title"] = "Member";  
  5. }  
  7. <div class="text-center">  
  8.     <h1 class="display-4">Welcome to Member page !!!!</h1>  
  9. </div>  
Next, we will add a new Menu in Layout view – header section,
  1. <header>  
  2.         <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">  
  3.             <div class="container">  
  4.                 <a class="navbar-brand" asp-area="" asp-page="/Index">RazorPages</a>  
  5.                 <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"  
  6.                         aria-expanded="false" aria-label="Toggle navigation">  
  7.                     <span class="navbar-toggler-icon"></span>  
  8.                 </button>  
  9.                 <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">  
  10.                     <ul class="navbar-nav flex-grow-1">  
  11.                         <li class="nav-item">  
  12.                             <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>  
  13.                         </li>  
  14.                         <li class="nav-item">  
  15.                             <a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>  
  16.                         </li>  
  17.                         <li class="nav-item">  
  18.                             <a class="nav-link text-dark" asp-area="" asp-page="/Member">Member</a>  
  19.                         </li>  
  20.                     </ul>  
  21.                 </div>  
  22.             </div>  
  23.         </nav>  
  24.     </header>  
Here we have added a new <li> for member and set the asp-page value as “\member”.
Let’s execute code and see the output,
Member menu has been added. Now click on Member Menu and see the output.
That’s all for this article. I hope you enjoy and learn something about the Layout view in Razor pages.