section的用途是什么

section的用途是什么

本文介绍了ASP.NET MVC:@section的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

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

推荐答案

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

@section is 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).

您可能会找到斯科特·古(Scott Gu)关于这个非常有趣的文章.

基于其他问题的说明

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

The @RenderSection syntax goes into the Shared View, such as:

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

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

This would then be placed in your view with @Section syntax:

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

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

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.

可以在_ViewStart.cshtml中设置常用视图设置,该视图定义了类似于以下内容的默认布局视图:

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

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

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

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

@{
    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.

这篇关于ASP.NET MVC:@section的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:22