Sie sind auf Seite 1von 2

Layout Page-

ASP.NET Core MVC provide ways to reuse and share visual elements and common code between different views.
These are 1) Layout Page 2) Start Page 3) Imports Page.

These are used to share common visual elements in your pages and provide a consistence look and feel throughout
your application.

Layout page is added to the Views/Shared folder and is named (as a convention) _Layout.cshtml. There can
be more than one layout pages in your application too.

Views have a Layout property through which they set the layout to use. The layout page is searched in controller
@RenderBodymethod to render the contents of a
specific folder and then in shared folder. Layout page calls
view.

Layout page can also use @RenderSection to decide where sections defined in views will be placed. These
@sectionsyntax. Layout page
sections can be required or optional. Views define the contents of a section using
can check if a section is defined in the view and act accordingly using IsSectionDefined method on the view:

Import Page

As we discussed in Razor post, views can use directives for a number of things, e.g. importing namespaces
(@using), injecting dependencies (@inject) and declaring model type (@model). MVC provides an import
page to declare directives common to one or more views.

Import page is added usually in Views folder and is named _ViewImports.cshtml . It can also be added to
other folders (e.g. controller specific views folder) however, in this case it will apply to views inside this folder (and
its subfolders).

In case of multiple import pages, either the directives close to the views are used (e.g. @model,@inject) or all
are combined (@using, @addTagHelper).

Start Page

MVC provides a mechanism to run code common to all views using a start page. Start page run before every view,
except for layout page and partial views.

Start page is added usually in Views folder and is named _ViewStart.cshtml . There can be multiple start
pages, in which case they will run in hierarchical order, from root to subfolders.

Start page is often used to set the Layout page for all the views in a folder.
Partial views –

Partial views are special type of views that are rendered inside other views. They are useful for reusing parts of a
view or splitting a large view into smaller components.

Partial views are created like regular views and can be returned via the controller action method
using ViewResult. The key difference is that they don’t run _ViewStart.cshtml before their rendering and are
usually rendered inside another view.

Partial views are rendered in views using @Html.Partial() method, passing in the name of partial view and
optionally a model. Partial view name can be absolute or relative path and view engine will search the current
folder or shared folder.

Partial View gets a copy of parent view’s ViewData dictionary. You could also pass a model into it, which is
normally part of parent’s page view model.

Areas-

MVC separate application concerns using Models, Views and Controllers. For larger applications Areas provide a
way to group these three concerns into another high level grouping. For instance you may want to split your
application into modules, each one containing its own MVC structure.

For routing purpose, there is another route parameter area available (in addition to controllerand action). You
could think of areas as namespaces, under which controllers live. The arearoute parameter is also available as
ambient value if it’s in context of current request, see

In order to use Areas in your project, you first setup a folder structure with Areas root folder and sub-folders for
each area (along with its controller, models and views). Note that the folder structure is important for views
because MVC will search for the views in the following sequence:

Das könnte Ihnen auch gefallen