Layout in ASP.NET Core Razor Pages

This project will demonstrate how to use Layout in ASP.NET Core Razor Pages

  • LearnASPNETCoreRazorPagesWithRealApps
    • Pages
      • Shared
        • _MyLayout.cshtml
      • Index.cshtml
      • Index.cshtml.cs
      • AboutUs.cshtml
      • AboutUs.cshtml.cs
      • News.cshtml
      • News.cshtml.cs
    • _ViewImports.cshtml
    • Startup.cs
<!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>
using Microsoft.AspNetCore.Mvc.RazorPages;

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

<h1>Home Page</h1>
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/_MyLayout.cshtml";
}

<h1>About Us Page</h1>
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/_MyLayout.cshtml";
}

<h1>News</h1>
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
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();
        }
    }
}

Screenshots