<!-- _Layout.cshtml -->
<!DOCTYPE html>
<html>
<head>
<!-- Common header content -->
</head>
<body>
@RenderBody()
<!-- Common footer content -->
</body>
</html>
<!-- Inside a view using the layout -->
@section content {
<p>This content will be rendered in the layout.</p>
}
@page
@model MyModel
@{
Layout = "_Layout";
}
<!-- In _ViewStart.cshtml -->
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<!-- In _Layout.cshtml -->
@RenderSection("sidebar", required: false)
<!-- In _Layout.cshtml -->
@section sidebar {
<div class="sidebar">Sidebar content goes here</div>
}
<!-- In a view -->
@section sidebar {
<p>Additional content for the sidebar.</p>
}
<!-- In _Layout.cshtml -->
@section sidebar {
<text>Content for the sidebar is required.</text>
}
@{
Layout = null;
}