Search code examples
razorrazor-pages

share _layout inherit in razor page


When I create the razor page, the default shared_layout.cshtml will be

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - xmc.Alliance.CementTrans.Web</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
    <link rel="stylesheet" href="~/xmc.Alliance.CementTrans.Web.styles.css" asp-append-version="true" />
</head>
<body>
    <header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
            <div class="container">
                ...           
            </div>
        </nav>
    </header>
    <div class="container">
        <main role="main" class="pb-3">
            @RenderBody()
        </main>
    </div>

    <footer class="border-top footer text-muted">
        <div class="container">
            &copy; 2022 - xmc.Alliance.CementTrans.Web - <a asp-area="" asp-page="/Privacy">Privacy</a>
        </div>
    </footer>

    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>

    @await RenderSectionAsync("Scripts", required: false)
</body>
</html>

I want to create the shared_globallayout.cshtml which just want add all stylesheet and javascript only, and then create the childlayout which base on _globallayout.cshtml and add back <nav class="navbar> and

So when I create some custom page (e.g. login page) which I can exclude nav bar and footer but can reuse css and javascript.

can I know how to do it ?

Thank you


Solution

  • Create _GlobalLayout.cshtml which should look like this:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>@ViewData["Title"] - xmc.Alliance.CementTrans.Web</title>
        <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
        <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
        <link rel="stylesheet" href="~/xmc.Alliance.CementTrans.Web.styles.css" asp-append-version="true" />
    </head>
    <body>
        @RenderBody()
    
        <script src="~/lib/jquery/dist/jquery.min.js"></script>
        <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
        <script src="~/js/site.js" asp-append-version="true"></script>
    
        @await RenderSectionAsync("Scripts", required: false)
    </body>
    </html>
    

    and _Layout.cshtml should look like this:

    @{
        Layout = "_GlobalLayout";
    }
    <header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
            <div class="container">
                ...           
            </div>
        </nav>
    </header>
    <div class="container">
        <main role="main" class="pb-3">
            @RenderBody()
        </main>
    </div>
    
    <footer class="border-top footer text-muted">
        <div class="container">
            &copy; 2022 - xmc.Alliance.CementTrans.Web - <a asp-area="" asp-page="/Privacy">Privacy</a>
        </div>
    </footer>
    

    and that's it.