加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 00:46:50 所属栏目:asp.Net 来源:网络整理
导读:在MVC / Razor语法中,我试图理解为什么我们需要@RenderBody。 例如(从example开始的代码) html head meta charset="utf-8" / titleMy WebSite/title style #container { width: 700px; } #left { float: left; width: 150px; } #content { padding: 0 210px
在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,并提供一个更容易调用的功能。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读