liveBook · Manning

Layout property

As explained in Pro ASP.NET Core 7, Tenth Edition

The Layout property is a Razor view property that specifies the layout used to render the view or null when no layout is required.

The Layout property controls which Razor layout wraps a view. A layout holds shared HTML, while @RenderBody() receives the view-specific content. Assign a layout name without a path or extension; Razor searches the controller and Shared view folders. section 22.4

Select a layout in a view

A view can select a shared layout directly: @{ Layout = "_Layout"; }. Its remaining markup is inserted into the layout's @RenderBody() position. A _ViewStart.cshtml file can set the same value once for a folder, avoiding repetition across views. section 22.4 section 22.4.2

Override the default when the view's rendering needs differ:

  • Assign another layout name in the view; the view value overrides _ViewStart.cshtml. section 22.4.3
  • Assign an expression, such as a product price condition, to choose between layouts programmatically. section 22.4.3
  • Set Layout = null when the view already contains a complete HTML document; otherwise the result can contain nested document structure. section 22.4.3
  • Use a controller-specific _ViewStart.cshtml to apply a different default across one controller's views. section 22.4.3
Listing 22.10 Using a layout in the Index.cshtml file in the Views/Home folder
@model Product?
@{
    Layout = "_Layout";
}
<div class="m-2">
    <table class="table table-sm table-striped table-bordered">
        <tbody>
            <tr><th>Name</th><td>@Model?.Name</td></tr>
            <tr>
                <th>Price</th>
                <td>
                    @Model?.Price.ToString("c")
                    (@(((Model?.Price / ViewBag.AveragePrice)
                            * 100).ToString("F2"))% of average price)     
                </td>
            </tr>
            <tr><th>Category ID</th><td>@Model?.CategoryId</td></tr>
        </tbody>
    </table>
</div>

Shows a view that assigns a layout while retaining only product-specific markup. section 22.4

The property is also available to Razor Pages. A Pages _ViewStart.cshtml supplies a default, while an individual page can set Layout = null. Layout files placed beside Razor Pages take precedence over files in the Shared folder. section 23.5.1

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains direct, default, conditional, controller-specific, and null assignments, but does not descend into Razor rendering internals.
Examples
Uses _Layout, _ImportantLayout, conditional product pricing, complete HTML documents, and controller or page-specific _ViewStart.cshtml files.
What it assumes
Assumes basic Razor view or Page structure and introduces layout files, _ViewStart.cshtml, RenderBody, and layout search locations.
What you can do after
Configure Razor rendering with shared, conditional, controller-specific, or disabled layouts and verify results through browser requests.
How it connects
Connects Razor views and Razor Pages with layouts, _ViewStart.cshtml, RenderBody, view models, and controller-specific folders.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.