微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net – RenderBody和RenderSection之间的区别

在MVC / Razor语法中,我试图理解为什么我们需要@RenderBody。

例如(从example开始的代码)

<html>
    <head>
        <Meta charset="utf-8" />
        <title>My WebSite</title>
        <style>
            #container { width: 700px; }
            #left { float: left; width: 150px; }
            #content { padding: 0 210px 0 160px; }
            #right { float: right; width: 200px; }
            .clear { clear: both; }
        </style>
    </head>
    <body>
        <div id="container">
            <div id="left">
                @RenderSection("left",required:false)
            </div>
            <div id="content">
                @RenderBody()
            </div>
            <div id="right">
                @RenderSection("right",required:false)
            </div>
            <div class="clear"></div>
        </div>
    </body>
</html>


@{
      Layout = "~/_3ColLayout.cshtml";
}

<h1>Main Content</h1>

@section left {
    <h1>Left Content</h1>
}

@section right {
    <h1>Right Content</h1>
}

为什么我不能简单地使用@RenderSection的一切,像这样:

<div id="content">
     @RenderSection("Body",required:true)
</div>

@section Body{
    <h1>Body Content</h1>
}

解决方法

只是因为方便。渲染身体是你最有可能做的事情,所以它有一个专门的功能。让你不要为身体声明一个@section,并提供一个更容易调用功能

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐