ASP.NET Core: Layouts, Sections and ViewStart

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:03:00

Layout Page in ASP.NET Core:

A layout page in ASP.NET Core is a shared template that defines the common structure and design for multiple views in a web application. It typically includes elements like headers, footers, and navigation menus.


<!-- _Layout.cshtml -->
<!DOCTYPE html>
  <!-- Common header content -->
  <!-- Common footer content -->

Location of the Layout Page:

Layout pages are typically stored in the "Views/Shared" folder within an ASP.NET Core application, and they can be referenced by views throughout the application.

Create the Layout Page:

To create a layout page, you can simply add a new Razor view with the "_Layout" name and define the common structure and elements you want to include in your application's pages.


@RenderBody() is a placeholder in the layout page that gets replaced with the content of the view that uses the layout. It allows dynamic content injection into the layout.


<!-- Inside a view using the layout -->
@section content {
  <p>This content will be rendered in the layout.</p>

Specifying the Layout to Use in the View:

You can specify which layout to use for a view by setting the Layout property in the view's Razor page or by using the Layout parameter in the View() method.


@model MyModel
  Layout = "_Layout";

The Layout Path:

The layout path specifies where to find the layout page. It can be set in the _ViewStart.cshtml file or directly in the view.


<!-- In _ViewStart.cshtml -->
  Layout = "~/Views/Shared/_Layout.cshtml";


Sections in a layout page allow views to inject content into specific predefined areas of the layout. This helps customize different parts of the layout.


<!-- In _Layout.cshtml -->
@RenderSection("sidebar", required: false)

Adding a Section to Layout Page:

You can define a section in the layout page using the @section directive and a section name.


<!-- In _Layout.cshtml -->
@section sidebar {
  <div class="sidebar">Sidebar content goes here</div>

Defining the Section in View:

To populate a section in a view, use the @section directive with the same section name.


<!-- In a view -->
@section sidebar {
  <p>Additional content for the sidebar.</p>

Making the Section Required:

You can make a section required in the layout, which enforces that views using the layout must provide content for that section.


<!-- In _Layout.cshtml -->
@section sidebar {
  <text>Content for the sidebar is required.</text>


ViewStart.cshtml is a special file that can be used to define common settings for views, such as the layout to be used by default. Removing the Layout from a View: To remove the layout from a specific view, set the Layout property to null in the view.


  Layout = null;

Understanding MVC Pattern:

MVC (Model-View-Controller) is a design pattern used in ASP.NET Core to separate an application into three interconnected components: Model (data and business logic), View (presentation and user interface), and Controller (handles user input and coordinates Model and View).

Action Result:

Action results in ASP.NET Core are the responses returned from controller methods. They represent the data to be displayed in a view or sent to the client, such as HTML, JSON, or redirects.

Model-View-Controller Communication:

In the MVC pattern, the Model communicates with the Controller to provide data and responds to requests. The Controller handles user input, processes it, and updates the Model. The Controller selects the View to display, and the View can access data from the Model to render the UI.
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
Accept cookies & close this