In ASP.NET MVC, layout pages are used to define a common structure for multiple views within an application. They allow developers to create a consistent look and feel across different pages by providing a shared template that can include common elements such as headers, footers, navigation menus, and stylesheets. Layout pages help to reduce code duplication and improve maintainability by centralizing the layout of the application.

Key Features of Layout Pages

Layout pages offer several key features:

  • Consistent Structure: Layout pages provide a consistent structure for all views, ensuring that common elements are rendered uniformly across the application.
  • Code Reusability: By defining a layout page, you can reuse the same layout for multiple views, reducing redundancy and improving maintainability.
  • Section Definitions: Layout pages can define sections that can be overridden by individual views, allowing for flexibility in content rendering.

Creating a Layout Page

To create a layout page in an ASP.NET MVC application, follow these steps:

  1. Right-click on the Views/Shared folder in your project.
  2. Select Add > View.
  3. In the "Add View" dialog, enter a name for the layout page (e.g., _Layout.cshtml) and click Add.

Sample Code for a Layout Page

Below is an example of a simple layout page named _Layout.cshtml:

        
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link rel="stylesheet" href="~/Content/Site.css" />
</head>
<body>
<header>
<h1>My ASP.NET MVC Application</h1>
<nav>
<ul>
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Products", "Index", "Product")</li>
</ul>
</nav>
</header>

<div class="container">
@RenderBody() <!-- This is where the content of the views will be rendered -->
</div>

<footer>
<p>© @DateTime.Now.Year - My ASP.NET MVC Application</p>
</footer>
</body>
</html>

In this example:

  • The layout page defines a basic HTML structure, including a header, navigation menu, content area, and footer.
  • The @RenderBody() method is used to specify where the content of the individual views will be inserted within the layout.
  • Links to other pages are created using @Html.ActionLink, which generates the appropriate URLs based on the specified controller and action.

Using a Layout Page in a View

To use the layout page in a view, you need to specify the layout at the top of the view file. Below is an example of a view named Index.cshtml that uses the layout page:

        
@{
Layout = "~/Views/Shared/_Layout.cshtml"; // Specify the layout page
}

<h2>Welcome to the Home Page</h2>
<p>This is the content of the home page.</p>

In this example:

  • The Layout property is set to the path of the layout page, indicating that this view should use the specified layout.
  • The content of the view will be rendered in place of the @RenderBody() method in the layout page.

Defining Sections in Layout Pages

Layout pages can also define sections that can be overridden by individual views. This allows you to inject additional content into specific areas of the layout. Here’s how to define and use sections in a layout page:

        
<body>
<header>
<h1>My ASP.NET MVC Application</h1>
<nav>
<ul>
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Products", "Index", "Product")</li>
</ul>
</nav>
</header>

<div class="container">
@RenderBody() <!-- Main content -->
@RenderSection("Scripts", required: false) <!-- Optional section for scripts -->
</div>

<footer>
<p>© @DateTime.Now.Year - My ASP.NET MVC Application</p>
</footer>
</body>

In this example, the layout page defines an optional section named Scripts using @RenderSection. This section can be used to include additional scripts specific to a view.

Using Sections in a View

To use the defined section in a view, you can override it as follows:

        
@{
Layout = "~/Views/Shared/_Layout.cshtml"; // Specify the layout page
}

<h2>Welcome to the Home Page</h2>
<p>This is the content of the home page.</p>

@section Scripts {
<script src="~/Scripts/home.js"></script> <!-- Custom script for this view -->
}

In this example:

  • The @section Scripts block is defined in the view, allowing you to include a custom script that will be rendered in the layout page where @RenderSection("Scripts") is called.

Conclusion

Layout pages are an essential feature in ASP.NET MVC that help maintain a consistent structure across multiple views. By using layout pages, you can reduce code duplication, improve maintainability, and create a more organized application. The ability to define sections further enhances the flexibility of layout pages, allowing for dynamic content injection as needed.