Using Shared Layouts with Razor Pages
Learn about utilizing ASP.NET Core layouts for efficient management, demonstrating through creating a layout.
We'll cover the following
Most websites have more than one page. If every page had to contain all of the current boilerplate markup in index.cshtml
, that would become a pain to manage. So, ASP.NET Core has a feature named layouts
.
Method to use layouts
To use layouts, we must create a Razor file to define the default layout for all Razor Pages (and all MVC views) and store it in a Shared
folder to easily find it by convention. The name of this file can be anything because we will specify it, but _Layout.cshtml
is good practice. We must also create a specially named file to set the default layout file for all Razor Pages (and all MVC views). This file must be named _ViewStart.cshtml
.
Layouts in action
Let’s see layouts in action:
Step 1: In the Pages
folder, add a file named _ViewStart.cshtml
. (The Visual Studio item template is named Razor View Start.)
Step 2: If you are using Visual Studio Code, modify the _ViewStart.cshtml
file content, as shown in the following markup:
Get hands-on with 1400+ tech skills courses.