有什么方法可以将第二个Layout与父Layout一起用于特定控制器?

时间:2018-11-22 21:57:41

标签: razor asp.net-core-mvc

如何为特定控制器将第二个布局与父布局一起使用。 在ASP.NET MVC应用中,我们为所有视图提供了父级布局

_ViewStart

@{
    Layout = "_Layout";
 }

现在我有Controller调用XcxController,对于XcxController的所有视图,我想与父XcxLayout一起使用名为Layout的另一种布局。

有可能吗?我该如何实现?

1 个答案:

答案 0 :(得分:1)

是的,您可以使用多个嵌套版式,包括版式内的版式:

Index.cshtml:

@{ 
    Layout = "_Layout";
}
<h1>This is normal index page</h1>

Layout.cshtml:

<h2>with normal layout</h2>
@RenderBody()

PageWithNestedLayout.cshtml:

@{
    Layout = "_NestedLayout";
}
<h1>This is another page</h1>

_NestedLayout.cshtml:

@{
    Layout = "_Layout";
}
<h2>with a nested layout and:</h2>
@RenderBody()