.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
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?
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
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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
None
0 Points
3 Posts
Multiple layouts
Feb 02, 2021 06:34 PM|V74|LINK
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";
}
Participant
980 Points
319 Posts
Re: Multiple layouts
Feb 03, 2021 01:53 AM|Jerry Cai|LINK
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:
Result:
Best Regards,
Jerry Cai
None
0 Points
3 Posts
Re: Multiple layouts
Feb 03, 2021 02:08 PM|V74|LINK
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!!
Participant
980 Points
319 Posts
Re: Multiple layouts
Feb 04, 2021 07:09 AM|Jerry Cai|LINK
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
None
0 Points
3 Posts
Re: Multiple layouts
Feb 04, 2021 07:49 PM|V74|LINK
Hi Jerry
It s working perfectly!!
Thanks a lot
Best regards
Victor