这是默认MVC 3模板中的About.cshtml:
@{
ViewBag.Title = "About Us";
}
<h2>About</h2>
<p>
Put content here.
</p>
Run Code Online (Sandbox Code Playgroud)
我希望能找到对_ViewStart文件的引用About.cshtml,但显然不是.
我看着在global.asax和web.config,但我不能找出如何将About.cshtml文件"链接"从_ViewStart文件的布局.
一切都按预期工作,我只想知道引擎盖下发生了什么......
我有一个顶级的_Layout.cshtml,看起来像这样:
<html>
<head>
@RenderSection("Header", required: false)
</head>
<body>
@RenderSection("LeftPane", required: false)
@RenderSection("RightPane", required: false)
@RenderBody()
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
然后我有两个"子布局".一个只定义LeftPane部分,另一个定义LeftPane和RightPane.这些子布局称为_LeftPane.cshtml和_LeftPlusRightPane.cshtml,它们的布局设置为"_Layout.cshtml".
然后在每个View .cshtml文件中,我将Layout设置为_LeftPane.cshtml或_LeftPlusRightPane.cshtml,具体取决于我想要在页面上显示的内容.
一切正常.问题出在我<head>在文档部分添加的新"标题"部分.此部分未在子布局中定义,而是在实际视图中定义.当我尝试以这种方式查看某些内容时,我收到错误:
已定义以下部分,但尚未针对布局页面"〜/ Views/Shared/_LeftPlusRightPane.cshtml":"Header"进行渲染.
我不想在子布局中渲染Header部分,我想在_Layout.cshtml文件中渲染它.如何从低级视图,子布局到顶部_Layout"通过"定义的标题部分?