Use Nested Layout in ASP.NET Core Razor Pages

On the Visual Studio, create new ASP.NET Core Web Application project

Select Empty Template

Click Ok button to Finish




Open Startup.cs file and add new configurations as below:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace LearnASPNETCoreRazorPagesWithRealApps
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles();

            app.UseMvc();
        }
    }
}

Select Views folder and right click to select Add\New Item Menu

Select Web\ASP.NET in left side. Select Razor View Imports item and click Add button to Finish

In _ViewImports.cshtml file and TagHelpers library as below:

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers




Create new folder named Pages. In Pages folder, create new folder named Shared. In Shared folder, create Razor Layout named _MasterLayout.cshtml as below:

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
</head>
<body>

    <a asp-page="index">Home</a> |
    <a asp-page="aboutus">About Us</a> |
    <a asp-page="news">News</a>
    <br /><br />
    @RenderBody()
    <br /><br />
    Copyright PMK Lab

</body>
</html>

In Shared folder, create Razor Layout named _AboutUsLayout.cshtml as below:

@{
    ViewData["Title"] = "About Us";
    Layout = "~/Pages/Shared/_MasterLayout.cshtml";
}

<a asp-page="about1">About 1</a> |
<a asp-page="about2">About 2</a>
<br /><br />
@RenderBody()




In Shared folder, create Razor Layout named _NewsLayout.cshtml as below:

@{
    ViewData["Title"] = "News";
    Layout = "~/Pages/Shared/_MasterLayout.cshtml";
}

<a asp-page="news1">News 1</a> |
<a asp-page="news2">News 2</a> |
<a asp-page="news3">News 3</a>
<br /><br />
@RenderBody()

In Pages folder, create new Razor Page named Index as below:

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
    public class IndexModel : PageModel
    {
        public void OnGet()
        {
        }
    }
}
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.IndexModel
@{
    ViewData["Title"] = "Index";
    Layout = "~/Pages/Shared/_MasterLayout.cshtml";
}

<h1>Home Page</h1>




In Pages folder, create new Razor Page named AboutUs as below:

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
    public class AboutUsModel : PageModel
    {
        public void OnGet()
        {
        }
    }
}
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.AboutUsModel
@{
    ViewData["Title"] = "About Us";
    Layout = "~/Pages/Shared/_AboutUsLayout.cshtml";
}

<h1>About Us</h1>

In Pages folder, create new Razor Page named About1 as below:

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
    public class About1Model : PageModel
    {
        public void OnGet()
        {
        }
    }
}
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.About1Model
@{
    ViewData["Title"] = "About 1";
    Layout = "~/Pages/Shared/_AboutUsLayout.cshtml";
}

<h1>About 1</h1>




In Pages folder, create new Razor Page named About2 as below:

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
    public class About2Model : PageModel
    {
        public void OnGet()
        {
        }
    }
}
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.About2Model
@{
    ViewData["Title"] = "About 2";
    Layout = "~/Pages/Shared/_AboutUsLayout.cshtml";
}

<h1>About 2</h1>




In Pages folder, create new Razor Page named News as below:

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
    public class NewsModel : PageModel
    {
        public void OnGet()
        {
        }
    }
}
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.NewsModel
@{
    ViewData["Title"] = "News";
    Layout = "~/Pages/Shared/_NewsLayout.cshtml";
}

<h1>News</h1>

In Pages folder, create new Razor Page named News1 as below:

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
    public class News1Model : PageModel
    {
        public void OnGet()
        {
        }
    }
}
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.News1Model
@{
    ViewData["Title"] = "News 1";
    Layout = "~/Pages/Shared/_NewsLayout.cshtml";
}

<h1>News 1</h1>




In Pages folder, create new Razor Page named News2 as below:

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
    public class News2Model : PageModel
    {
        public void OnGet()
        {
        }
    }
}
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.News2Model
@{
    ViewData["Title"] = "News 2";
    Layout = "~/Pages/Shared/_NewsLayout.cshtml";
}

<h1>News 2</h1>

In Pages folder, create new Razor Page named News3 as below:

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace LearnASPNETCoreRazorPagesWithRealApps.Pages
{
    public class News3Model : PageModel
    {
        public void OnGet()
        {
        }
    }
}
@page
@model LearnASPNETCoreRazorPagesWithRealApps.Pages.News3Model
@{
    ViewData["Title"] = "News 3";
    Layout = "~/Pages/Shared/_NewsLayout.cshtml";
}

<h1>News 3</h1>




Open Index Razor Page with following url: http://localhost:1115

Open AboutUs Razor Page with following url: http://localhost:1115/AboutUs

Open About1 Razor Page with following url: http://localhost:1115/About1

Open About2 Razor Page with following url: http://localhost:1115/About2

Open News Razor Page with following url: http://localhost:1115/News

Open News1 Razor Page with following url: http://localhost:1115/News1

Open News2 Razor Page with following url: http://localhost:1115/News2

Open News3 Razor Page with following url: http://localhost:1115/News3