C# 这个rendersection的代码是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10764930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does this code of rendersection mean?
提问by Tim Tom
I am a beginner in Asp.Net MVC3. Can anybody please explain what is meant by this code:
我是 Asp.Net MVC3 的初学者。任何人都可以解释一下这段代码的含义:
@section head
{
@RenderSection("head", false)
}
On ScottGu's article:
在 ScottGu 的文章中:
http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx
http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx
there is an example of RenderSection but it defines @section and then somewhere @RenderSection is used. In this case section head is defined and within that itself the same head is being rendered which confused me.
有一个 RenderSection 示例,但它定义了 @section,然后在某处使用了 @RenderSection。在这种情况下,定义了部分头部,并且在其中呈现了相同的头部,这让我感到困惑。
What does RenderSection do and how do I find what is being rendered here?
RenderSection 有什么作用,我如何找到这里渲染的内容?
采纳答案by Brunner
Scott wrote at one point
斯科特曾写道
The first parameter to the “RenderSection()” helper method specifies the name of the section we want to render at that location in the layout template. The second parameter is optional, and allows us to define whether the section we are rendering is required or not. If a section is “required”, then Razor will throw an error at runtime if that section is not implemented within a view template that is based on the layout file (which can make it easier to track down content errors).
“RenderSection()”辅助方法的第一个参数指定了我们想要在布局模板中的那个位置渲染的部分的名称。第二个参数是可选的,它允许我们定义我们正在渲染的部分是否是必需的。如果某个部分是“必需的”,那么如果该部分未在基于布局文件的视图模板中实现,那么 Razor 将在运行时抛出错误(这可以更容易地跟踪内容错误)。
So, what RenderSection does, is rendering a section defined in the template/view (not the general _Layout). A little bit furtherdown under "Implementing the “SideBar” Section in our View Template" he explains how to implement a section.
因此,RenderSection 所做的是渲染模板/视图中定义的部分(不是一般的 _Layout)。在“在我们的视图模板中实现“SideBar”部分的后面,他解释了如何实现一个部分。
So all in all, what you have is a section called "head" that renders a section called "head" in a view that's further down/nested.
总而言之,您拥有的是一个名为“head”的部分,它在更向下/嵌套的视图中呈现名为“head”的部分。
Edit: have a look at http://blogs.msdn.com/b/marcinon/archive/2010/12/15/razor-nested-layouts-and-redefined-sections.aspxto see what I mean with nested views - but note that this article is over a year old now.
编辑:看看http://blogs.msdn.com/b/marcinon/archive/2010/12/15/razor-nested-layouts-and-redefined-sections.aspx看看我对嵌套视图的意思 -但请注意,这篇文章现在已经一年多了。
MasterLayout:
主布局:
@RenderSection("head", false)
SubLayout:
子布局:
@{
Layout = "~/Views/_MasterLayout.cshtml";
}
@section head
{
@RenderSection("head")
}
Content:
内容:
@{
Layout = "~/Views/_SubLayout.cshtml";
}
@section head
{
<title>Content-Layout</title>
}
回答by Kapil Khandelwal
You define the section in a view and render it in the _Layout.cshtml.
您在视图中定义该部分并在 _Layout.cshtml 中呈现它。
In your layout (master) page place this:
在您的布局(母版)页面中放置:
@RenderSection("head", false)
In your view page place this:
在您的视图页面中放置:
@section head {
PUT VIEW SPECIFIC CODE HERE
}
Here "head" is the name of section that you can define in your view page.
这里的“head”是您可以在视图页面中定义的部分的名称。
Its somewhat like ContentPlaceHolder that we use in asp.net webforms.
它有点像我们在 asp.net webforms 中使用的 ContentPlaceHolder。
回答by user3094826
By using @rendersection in your _Layout file, you have control of the layout and order of sections in you main .cshtml code (lets say its called index.cshtml)
通过在 _Layout 文件中使用 @rendersection,您可以控制主 .cshtml 代码中的部分的布局和顺序(假设它称为 index.cshtml)
For instance if you have an @RenderSection("scripts", false) at the end of your _Layout file, then even if the "scripts" section is placed at the top of your index.cshtml files, it will be rendered at the bottom. Thus ensuring that all script sections are loaded in a consistant manner throughout your application.
例如,如果您在 _Layout 文件的末尾有一个 @RenderSection("scripts", false),那么即使“scripts”部分位于 index.cshtml 文件的顶部,它也会在底部呈现. 从而确保在整个应用程序中以一致的方式加载所有脚本部分。
If in the future you decided for some reason to move all the scripts to the head section, you can easily do this by just moving one line of code in the _Layout file.
如果将来您出于某种原因决定将所有脚本移动到 head 部分,则只需移动 _Layout 文件中的一行代码即可轻松完成此操作。

