asp.net-mvc ASP.NET MVC:@section 的目的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13089489/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:22:17  来源:igfitidea点击:

ASP.NET MVC: What is the purpose of @section?

asp.net-mvc

提问by A Bogus

For an ASP.NET MVC application, I saw this blog article. The author ScottGu adds @sectionto the Index.cshtml.

对于 ASP.NET MVC 应用程序,我看到了这篇博客文章。作者 ScottGu 添加@section到 Index.cshtml。

I have a couple of questions (referring to the article above):

我有几个问题(参考上面的文章):

  • Is Index.cshtml a shared View?
  • The example code uses @sectioncode in a particular view. Why?
  • Index.cshtml 是共享视图吗?
  • 示例代码使用@section特定视图中的代码。为什么?

Can someone please explain why and when I would use @sectionin a View?

有人可以解释为什么以及何时@section在视图中使用吗?

回答by Frazell Thomas

@sectionis for defining a content are override from a shared view. Basically, it is a way for you to adjust your shared view (similar to a Master Page in Web Forms).

@section用于定义从共享视图覆盖的内容。基本上,它是您调整共享视图的一种方式(类似于 Web 窗体中的母版页)。

You might find Scott Gu's write up on this very interesting.

您可能会发现Scott Gu 对此的撰写非常有趣

Edit: Based on additional question clarification

编辑:基于额外的问题澄清

The @RenderSectionsyntax goes into the Shared View, such as:

@RenderSection语法进入共享视图,如:

<div id="sidebar">
    @RenderSection("Sidebar", required: false)
</div>

This would then be placed in your view with @Sectionsyntax:

然后将使用@Section语法将其放置在您的视图中:

@section Sidebar{
    <!-- Content Here -->
}

In MVC3+ you can either define the Layout file to be used for the view directly or you can have a default view for all views.

在 MVC3+ 中,您可以直接定义要用于视图的布局文件,也可以为所有视图设置默认视图。

Common view settings can be set in _ViewStart.cshtml which defines the default layout view similar to this:

通用视图设置可以在 _ViewStart.cshtml 中设置,它定义了与此类似的默认布局视图:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

You can also set the Shared View to use directly in the file, such as index.cshtml directly as shown in this snippet.

您还可以将共享视图设置为直接在文件中使用,例如 index.cshtml 直接如本代码段所示。

@{
    ViewBag.Title = "Corporate Homepage";
    ViewBag.BodyID = "page-home";
    Layout = "~/Views/Shared/_Layout2.cshtml";
}

There are a variety of ways you can adjust this setting with a few more mentioned in this SO answer.

您可以通过多种方式调整此设置,并在此 SO 答案中提及更多。

回答by John Mc

A good example is Javascript. You want this to be at the bottom of the page that is rendered in the browser because this is best practice.

一个很好的例子是 Javascript。您希望它位于浏览器中呈现的页面底部,因为这是最佳实践。

How would you do this from a View based on a Layout/Masterpage where you can only access the middle of the page?

您将如何从基于布局/母版页的视图中执行此操作,您只能访问页面的中间?

You do this by declaring a Scripts section at the bottom of the Layout page. Then you can add content, in this case Javascript includes (I hope!), from your View page to the bottom of your layout page.

您可以通过在 Layout 页面底部声明一个 Scripts 部分来做到这一点。然后您可以添加内容,在这种情况下,Javascript 包括(我希望!),从您的视图页面到布局页面的底部。

回答by chrislhardin

It lets you define a @Sectionof code in your template that you can then include in other files. For example, a sidebar defined in the template, could be referenced in another included view.

它允许您@Section在模板中定义代码,然后您可以将其包含在其他文件中。例如,模板中定义的侧边栏可以在另一个包含的视图中引用。

//This could be used to render a @Section defined as @Section SideBar { ...
@RenderSection("SideBar", required: false);

Hope this helps.

希望这可以帮助。

回答by ischell

You want to use sections when you want a bit of code/content to render in a placeholder that has been defined in a layout page.

当您希望在布局页面中定义的占位符中呈现一些代码/内容时,您希望使用部分。

In the specific example you linked, he has defined the RenderSection in the _Layout.cshtml. Any view that uses that layout can define an @section of the same name as defined in Layout, and it will replace the RenderSection call in the layout.

在您链接的特定示例中,他在 _Layout.cshtml 中定义了 RenderSection。任何使用该布局的视图都可以定义一个与 Layout 中定义的名称相同的 @section,它将替换布局中的 RenderSection 调用。

Perhaps you're wondering how we know Index.cshtml uses that layout? This is due to a bit of MVC/Razor convention. If you look at the dialog where he is adding the view, the box "Use layout or master page" is checked, and just below that it says "Leave empty if it is set in a Razor _viewstart file". It isn't shown, but inside that _ViewStart.cshtml file is code like:

也许您想知道我们如何知道 Index.cshtml 使用该布局?这是由于一些 MVC/Razor 约定。如果您查看他添加视图的对话框,则会选中“使用布局或母版页”框,在其下方显示“如果在 Razor _viewstart 文件中设置,则留空”。它没有显示,但在 _ViewStart.cshtml 文件中的代码如下:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

The way viewstarts work is that any cshtml file within the same directory or child directories will run the ViewStart before it runs itself.

viewstarts 的工作方式是,同一目录或子目录中的任何 cshtml 文件都将在 ViewStart 运行之前先运行它。

Which is what tells us that Index.cshtml uses Shared/_Layout.cshtml.

这就是告诉我们 Index.cshtml 使用 Shared/_Layout.cshtml 的原因。