You're reading from Real-World Web Development with .NET 9Build websites and services using mature and proven ASP.NET Core MVC, Web API, and Umbraco CMS
Let's review how we can build the user interface of a web page in a modern ASP.NET Core MVC website.
Using shared layouts with Razor views
Most websites have more than one page. If every page had to contain all of the boilerplate markup that is currently in index.cshtml, that would become a pain to manage. So, ASP.NET Core has a feature named layouts. These can reduce code duplication and improve maintainability.
To use layouts, we must create a Razor file to define the default layout for all Razor views and store it in a Shared folder so that it can be easily found by convention. The name of this file can be anything, because we will specify it, but _Layout.cshtml is good practice. We must also have a specially named file to set the default layout file for all Razor views. This file must be named _ViewStart.cshtml.
The _ViewStart.cshtml file is used to define settings or behaviors like layout assignment that should apply to all views...
The rest of the chapter is locked
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of