liveBook · Manning

_ViewStart.cshtml

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

A Razor view start file, typically _ViewStart.cshtml, specifies common settings such as the layout for views or pages in its scope.

_ViewStart.cshtml sets a default layout for Razor views, so individual views can contain only their own content instead of repeating an HTML document or a Layout assignment. The layout supplies shared markup and inserts the view’s content at @RenderBody(). section 22.4.2 section 7.1.6

Set the default for controller views

Create Views/_ViewStart.cshtml containing `@{ Layout = "_Layout"; }`. Create Views/Shared/_Layout.cshtml with the common HTML document and `@RenderBody()` where view content belongs. A view such as Views/Shared/Common.cshtml can then contain just its own markup. In Visual Studio, the Razor View Start template creates the file; in Visual Studio Code, add it manually. section 22.4.2 section 7.1.6

Choose the file location and override only when needed:

  • Put _ViewStart.cshtml in Pages to set a default for Razor Pages. Pages have their own configuration files, so their default can differ from the controller views’ default. section 8.2.1
  • Put another view start file in Views/[controller] when every action of one controller needs a different layout. section 22.4.3
  • Assign Layout in an individual view to select a different layout, including one chosen by an expression. Set `Layout = null;` when the view or page supplies its own complete HTML document. section 22.4.3 section 23.5.1

Do not confuse _ViewStart.cshtml with _ViewImports.cshtml. The former selects a default layout; the latter makes namespaces and tag helpers available without repeating their directives in each view or page. section 32.5 section 32.6

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link href="/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
    <h6 class="bg-primary text-white text-center m-2 p-2">Layout</h6>
<!DOCTYPE html>
<html>
<head>
    <link href="/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
    <h6 class="bg-secondary text-white text-center m-2 p-2">Products</h6>
    <div class="m-2">
        <table class="table table-sm table-striped table-bordered">
            <thead><tr><th>Name</th><th>Price</th></tr></thead>
            <tbody>
                        
                <!-- ...table rows omitted for brevity... >
                                
            </tbody>
        </table>
    </div>
</body>
</html>
</body>
</html>

Inspect the response with a full HTML view and an active default layout: the resulting document contains nested document structure. Set Layout to null in that view instead. section 22.4.3

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

How deep it goes
Shows file placement, Layout assignment, scoped defaults, per-view overrides, and the malformed HTML caused by wrapping a complete document.
Examples
Uses Views/_ViewStart.cshtml, Pages/_ViewStart.cshtml, controller-specific layouts, and a full-document view that produces malformed HTML.
What it assumes
Assumes familiarity with Razor views or pages while showing how to create the view start file and assign its layout.
What you can do after
Operate/configure it to set shared layouts, override them for particular views, or disable them for complete HTML documents.
How it connects
Connects Razor layouts, controller views, Razor Pages, @RenderBody, and _ViewImports.cshtml through their distinct rendering and configuration roles.

Also explained in

Compare how 2 books explain _ViewStart.cshtml →

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.