MVC Child Master page with Razor

被刻印的时光 ゝ 提交于 2019-12-23 05:23:52

问题


I have an old MVC2 project that I gradulately convert to MVC 5, with Razor.

As the razor views with ASP Master Pages is not possible, I decided to duplicate the Site.Master into _Layout.cshtml.

Everything is OK But there is a problem, however.

I have an other Master page, TwoColumnsLayout.Master, a kind of "child" master page, that begins like this:

<%@ Master MasterPageFile="~/Views/Shared/Site.Master" Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

<asp:Content ContentPlaceHolderID="MetaContent" runat="server">
    <asp:ContentPlaceHolder ID="MetaContent" runat="server" />
</asp:Content>

<asp:Content ContentPlaceHolderID="HeadContent" runat="server">
    <asp:ContentPlaceHolder ID="HeadContent" runat="server" />
</asp:Content>

<asp:Content ContentPlaceHolderID="MainContent" runat="server">
    <div class="breadcrumbs">
    ...

How is possible to convert and use this one with the Razor engine?


回答1:


utilize @{ Layout = ""; } feature.

For Example:

~/Views/Shared/_LayoutMain.cshtml:

<html>
<head>
    <link href="..." rel="stylesheet" type="text/css" />
</head>
<body>
    <!-- header markup -->
    @RenderBody()
    <!-- footer markup -->
</body>
</html>

~/Views/Shared/_LayoutChild.cshtml:

@{
    Layout = "~/Views/Shared/_LayoutMain.cshtml";
}
<!-- Child Layout content -->
<div>
    @RenderBody()
</div>

~/Views/MyPage/_SomeViewPage.cshtml:

@{
    Layout = "~/Views/Shared/_LayoutChild.cshtml";
}

<div>
    @RenderBody()
</div>



回答2:


You just need to use nested layout templates instead.

Create another layout _TwoColumnsLayout.cshtml with the following:

@{
    Layout = "~/Shared/_Layout.cshtml";
}

//Nested specific markup ...

@RenderBody()

Then in your view specify your new nested template as follows:

@{
    Layout = "~/Shared/_TwoColumnsLayout.cshtml.cshtml";
}


来源:https://stackoverflow.com/questions/22506755/mvc-child-master-page-with-razor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!