ASP.NET MVC 3 嵌套母版页 - 如何在 cshtml 视图中使用它?

发布于 2024-11-05 03:35:58 字数 2526 浏览 1 评论 0原文

我有这 2 个母版页 - Site.Master 和 Blog.Master 我想知道如何在我的 cshtml 视图中使用 Blog.Master?

Site.Master:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html>
<html>
<head runat="server">
   <title>
     <asp:ContentPlaceHolder ID="phPageTitle" runat="server" />
   </title>
</head>
<body>
   <div>
      <div style="clear:both">
            <asp:ContentPlaceHolder ID="phAppMenu" runat="server">
            </asp:ContentPlaceHolder>
         </div>         
      </div>

      <div style="clear:both;min-height:500px">
         <asp:ContentPlaceHolder ID="phContent" runat="server">       
         </asp:ContentPlaceHolder>
      </div>

      <div style="clear:both">
         <asp:ContentPlaceHolder ID="phFooter" runat="server">       
         </asp:ContentPlaceHolder>
      </div>
   </div>
</body>
</html>

这是 blog.master:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" MasterPageFile="~/Views/Shared/Masters/Site.Master" %>
   <asp:ContentPlaceHolder ID="iphPageTitle" ContentPlaceHolderID="phPageTitle" runat="server">
      This is the page title
   </asp:ContentPlaceHolder>

   <asp:ContentPlaceHolder ID="iphTopMenu" ContentPlaceHolderID="phTopMenu" runat="server">
      Home | Browse | Search | Post | New | Featured | Recommended
   </asp:ContentPlaceHolder>

   <asp:ContentPlaceHolder ID="iphContent" ContentPlaceHolderID="phContent" runat="server">
      Main Content goes here
   </asp:ContentPlaceHolder>

   <asp:ContentPlaceHolder ID="iphFooter" ContentPlaceHolderID="phFooter" runat="server">
      This is the footer
   </asp:ContentPlaceHolder>

这是我想在其中使用 Blog.Master 的 Post.cshtml:

@model MyApp.Models.ShowPostViewModel
@{
    ViewBag.Title = Model.Post.Title;
}
<section>
    <article>        
        <div style="overflow:hidden">
            <div style="overflow:hidden">
               <header>
                  <b>@Model.Post.Title</b><br />
                  Author: @Model.Post.Author | Id: @Model.Post._id
               </header>
               @Html.Raw(Model.Post.Content)               
               </div>
            </div>
    </article>
</section>

I have these 2 master pages - Site.Master and Blog.Master
I want to know how can I use the Blog.Master in my cshtml view?

Site.Master:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html>
<html>
<head runat="server">
   <title>
     <asp:ContentPlaceHolder ID="phPageTitle" runat="server" />
   </title>
</head>
<body>
   <div>
      <div style="clear:both">
            <asp:ContentPlaceHolder ID="phAppMenu" runat="server">
            </asp:ContentPlaceHolder>
         </div>         
      </div>

      <div style="clear:both;min-height:500px">
         <asp:ContentPlaceHolder ID="phContent" runat="server">       
         </asp:ContentPlaceHolder>
      </div>

      <div style="clear:both">
         <asp:ContentPlaceHolder ID="phFooter" runat="server">       
         </asp:ContentPlaceHolder>
      </div>
   </div>
</body>
</html>

Here is blog.master:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" MasterPageFile="~/Views/Shared/Masters/Site.Master" %>
   <asp:ContentPlaceHolder ID="iphPageTitle" ContentPlaceHolderID="phPageTitle" runat="server">
      This is the page title
   </asp:ContentPlaceHolder>

   <asp:ContentPlaceHolder ID="iphTopMenu" ContentPlaceHolderID="phTopMenu" runat="server">
      Home | Browse | Search | Post | New | Featured | Recommended
   </asp:ContentPlaceHolder>

   <asp:ContentPlaceHolder ID="iphContent" ContentPlaceHolderID="phContent" runat="server">
      Main Content goes here
   </asp:ContentPlaceHolder>

   <asp:ContentPlaceHolder ID="iphFooter" ContentPlaceHolderID="phFooter" runat="server">
      This is the footer
   </asp:ContentPlaceHolder>

Here is my Post.cshtml where I want to use the Blog.Master:

@model MyApp.Models.ShowPostViewModel
@{
    ViewBag.Title = Model.Post.Title;
}
<section>
    <article>        
        <div style="overflow:hidden">
            <div style="overflow:hidden">
               <header>
                  <b>@Model.Post.Title</b><br />
                  Author: @Model.Post.Author | Id: @Model.Post._id
               </header>
               @Html.Raw(Model.Post.Content)               
               </div>
            </div>
    </article>
</section>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜肉鲜肉永远不皱 2024-11-12 03:35:59

不直接支持将 aspx master 与 razor 视图一起使用,但有一些解决方法。更多信息请参见:http://www.hanselman.com/blog/MishingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx

Using an aspx master with a razor view is not directly supported, but there are workarounds. More info here: http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文