将缩进添加到" @RenderBody()"

时间:2017-03-13 08:23:14

标签: asp.net-mvc asp.net-mvc-4 razor

有没有办法在_layout.cshtml {{}}}中添加缩进以使源代码更漂亮?

问题是在@RenderBody()无效之前在_layout.cshtml页面中添加缩进。我必须在我的视图中添加缩进(@RenderBody()),我想将其移至" _layout.cshtml"

_layout.cshtml:

index.cshtml

index.cshtml

<html>
   <body>
      <div>
         @RenderBody()
      </div>
    </body>
</html>

网页浏览源:

@{
     Layout = "~/Views/Shared/_TestLayout.cshtml";
}
<h2>Index</h2>

我的解决方案是将 index.cshtml 添加为缩进,如下代码所示:

<html>
    <body>
       <div>
<h2>Index</h2> <!-- problem here -->
       </div>
    </body>
</html>

有没有办法将其移至 _layout.cshtml:

1 个答案:

答案 0 :(得分:1)

使用APS.NET xxx渲染引擎无法实现。

然而,这是我的解决方案,等待更好的解决方案。

您可以像这样编写 index.cshtml

@{
    Layout = "~/Views/Shared/_TestLayout.cshtml";
}
@* fake indent level 0 *@
    @* fake indent level 1 *@
        @* fake indent level 2 *@
            <h2>Index</h2>
        @* fake indent level 2 (end) *@
    @* fake indent level 1 (end) *@
@* fake indent level 0 (end) *@

@* fake Razor comments *@用于标记 cshtml 代码中的缩进。

它不是很漂亮,但你会在你的页面视图来源中有这个:

<html>
    <body>
        <div>
            <h2>Index</h2>
        </div>
    </body>
</html>