locked
Multiple layouts RRS feed

  • Question

  • User-1836194432 posted

    I am using in viewstarts.cshtml file but i want to use 2 layouts. One named     _Layout     and the second    _my_Layout.    What is the best way?

    Layout = "_Layout

    My page:

    @{
        <title>@ViewData["Title"]page</title>
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    Tuesday, February 2, 2021 6:34 PM

All replies

  • User1312693872 posted

    Hi,V74

    You mean use two different Layout pages in a project?

    You can create two Layout files and call them in the views, like:

    first view:
    @{
        ViewData["Title"] = "Home Page";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    second view:
    @{
        ViewData["Title"] = "Privacy Policy";
        Layout = "~/Views/Shared/_my_Layout.cshtml";
    }

    Result:

    Best Regards,

    Jerry Cai

    Wednesday, February 3, 2021 1:53 AM
  • User-1836194432 posted

    Hi Jerry

    Thanks for your time!!

    Yes 2 layouts is the same project. Because one  _layout   is related to a database (real estate business. However I want to create also pages out of the database and I have some problems to find the solution.

    In your response you mean in viewstarts file correct? 

    This is what i have:

    viewstarts file:

    @{
        Layout = "_Layout";
    }

    This is what i have in main layout:

    <!DOCTYPE html>
    <html lang="en-En">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">

        <!-- THEME COLOR -->
        <meta name="theme-color" content="#ffffff">
        <!-- THEME COLOR -->

        @if (ViewBag.SeoPage == null)
        {
            <title>sometitle</title>
        }    

        else
        {
            @Html.Raw(ViewBag.SeoPage.HeaderTags);
            <title>@ViewBag.SeoPage.Title</title>;
        }

       @if (ViewBag.AppSettings != null && !string.IsNullOrEmpty(ViewBag.AppSettings.GoogleAnalyticsUrl) && !string.IsNullOrEmpty(ViewBag.AppSettings.GoogleAnalyticsTrackingID))
        {
          
             <script defer src="@ViewBag.AppSettings.GoogleAnalyticsUrl"></script>
            <script>
                window.dataLayer = window.dataLayer || [];
                function gtag() { dataLayer.push(arguments); }
                gtag('js', new Date());

                gtag('config', '@ViewBag.AppSettings.GoogleAnalyticsTrackingID');
            </script>
        }

        <!-- ICON - ICON APPLE apple.webp-->
        <link rel="icon" href="~/img/favicon.png" >
        <link rel="apple-touch-icon" href="~/img/apple.webp">
        <!-- ICON - ICON APPLE -->


        <!-- SITE - CONSULTANT  - PRELOAD -->
        <environment exclude="Development">
            <link rel="preload" href="~/css/site.min.css" as="style">
            <link rel="stylesheet" href="~/css/site.min.css" type="text/css" media="print" onload="this.media='all'; this.onload = null"/>

       and so on....

    This is what i have in some pages:

    @{
        ViewData["Title"] = "onetitle";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    The problem is all the pages are coming from a database (azure made with angular) and i need to create a blog. For that i don't want to use the database. I have tried different approaches but so far i can not find the way

    Thanks again!!

    Wednesday, February 3, 2021 2:08 PM
  • User1312693872 posted

    Hi,V74

    I mean you can define different views with different Layouts,

    For example, index view :

    @{
        ViewData["Title"] = "Home Page";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <div class="text-center">
        <h1 class="display-4">Welcome</h1>
        <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
    </div>

    Privacy view:

    @{
        ViewData["Title"] = "Privacy Policy";
        Layout = "~/Views/Shared/_my_Layout.cshtml";
    }
    <h1>@ViewData["Title"]</h1>
    
    <p>Use this page to detail your site's privacy policy.</p>

    The the two pages will use different Layouts, index view will use _Layout.cshtml while privacy will use _my_Layout.cshtml, they don't interfere

    with each other, configure layout directly in view will override the settings in _viewStart.

    Best Regards,

    Jerry Cai

    Thursday, February 4, 2021 7:09 AM
  • User-1836194432 posted

    Hi Jerry

    It s working perfectly!!

    Thanks a lot

    Best regards

    Victor

    Thursday, February 4, 2021 7:49 PM