Layout View in ASP.NET MVC

In this section, we will will learn about the layouts and the layout view in ASP.NET MVC application.

In an application, there always exist contain common parts of the user interface which remains the same throughout the application like the header, navigation bars and the footer section.

These common views can be grouped together in the ASP.NET MVC Layout, so that we don't have to write the same code in every page. The layout view can be said to be the master page of the ASP.NET web application.

For example, consider an application user interface shown below, it contain Header, Left menu bar, right bar and footer section that remains same in every page with only the body section changing dynamically:

asp.net Layout View

The layout view enables us to define a common site template, which other views can inherit from. The layout view eliminates code duplication thus enhancing development speed and easy maintenance. The layout view for the above sample would contain a Header, Left bar, Right bar and Footer sections. It contains a placeholder for the body section that changes dynamically.

The layout view has same extension as other views, that is: .cshtml or .vbhtml. Layout views are shared across other views, so by convention, it is kept in the Shared folder of the View.

For example, when we created our first MVC application in the previous section, it also created _Layout.cshtml in the Shared folder as shown below.