在WEB窗体模式中,用惯了母版页,并且常有母版页嵌套的情况。
而在MVC模式下,对应母版页的,称作为布局页。默认的布局页为 ~/Views/Shared/_Layout.cshtml。默认每个页面都会嵌于其中,因为在~/Views/_ViewStart.cshtml里已经写好:
@{ Layout = "~/Views/Shared/_Layout.cshtml";}当然,这个应该可以改的,但一般没有必要。我的做法是,将_Layout.cshtml作为基本母版页,然后再在上面衍生出各种子母版页。以下分别是_Layout.cshtml和某一子布局页(_SingleContent_Layout.cshtml)的代码:
_Layout.cshtml
@using common = www.AppCode.Common;子母版页_SingleContent_Layout.cshtml@common.GetTitle(ViewBag.IndependentTitle,ViewBag.Title) @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/modernizr") @RenderSection("header", required: false)@Scripts.Render("~/bundles/jquery182") @RenderSection("scripts", required: false)@{Html.RenderAction("LoginInfo", "Partial");}@RenderBody()
@{ Layout = "~/Views/Shared/_Layout.cshtml";}@section header{ @Styles.Render("~/Content/SingleContent") @* 这是本子布局页的子页插入内容的入口,注意到没?它写在本子布局页插入母版页的入口处*@ @RenderSection("header", required: false)}@section Scripts { @RenderSection("scripts", required: false)}内容页index.cshtml@RenderBody()
@{ ViewBag.Title = "Service"; Layout = "~/Views/Shared/_SingleContent_Layout.cshtml";}服务条款
参考文章: