Following is the exact scenario in my ASP.NET MVC application:
There are two layout pages which are quite identical to each other. But one is having angular related attributes in "" tag, whereas other is a non-angular layout. In order to avoid duplicate markup in both razor layout files, I thought to create a partial view and share it across the layout pages.
Following is the partial view (razor), I have named it as "_LayoutPartial":
_LayoutPartial.cshtml
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
@Html.Partial("_LoginPartial")
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
The above partial view is shared in "_Layout.cshtml", and "_AngularLayout.cshtml" which are as per below:
_Layout.cshtml
<body>
@Html.Partial("_LayoutPartial")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
_AngularLayout.cshtml
<body ng-app="myAngularLab">
@Html.Partial("_LayoutPartial")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
When I try to run the MVC application, I get following error:
The file "~/Views/Shared/_LayoutPartial.cshtml" cannot be requested directly because it calls the "RenderBody" method.
The error message is very obvious, and it seems that we can use RenderBody method only in the master page, and not anywhere else. But I am keen to know how we can have two identical layout pages (with a little differences as shown in the example) by writing common code instead of keeping duplicate code in both layout pages?
<div class="container body-content"> @RenderBody() <hr /> <footer> <p>© @DateTime.Now.Year - My ASP.NET Application</p> </footer> </div>FROM common file and put it individually in both the Angular and _Layout file? - Unbreakable@Scripts&@Stylesdeclaration in child layouts. In this case,ng-app="myAngularLab"placed on adivinstead ofbody. - Tetsuya Yamamoto