您可以检查哪个控制器当前正在执行其操作并检查HomePage Controller:
@{ var controllerName = ViewContext.RouteData.Values["controller"].ToString(); var actionName = ViewContext.RouteData.Values["controller"].ToString(); } @if(controllerName == "HomePage" && actionName == "yourActionName") { @RenderBody() } else { <section class="container content-section text-center"> @RenderBody() </section> }
至于我,我不会使用条件。 它有点像 的 劈 强> 但 的 不解决 强> 。下次需要更改某些内容时,您会添加越来越多的IF?
使用两种布局。
_LayoutWithSection.cshtml:
// ... <section class="container content-section text-center"> @RenderBody </section> // ...
_Layout.cshtml:
// ... @RenderBody // ...
首页/ Index.cshtml:
@{ Layout = "~/Views/Shared/_LayoutWithSection.cshtml"; } // ...
_ViewStart.cshtml:
@{ Layout = "~/Views/Shared/_Layout.cshtml"; } // ...