ASP.NET Core MVC Beginner

Layouts, _ViewStart and _ViewImports

Most pages on a site share the same header, navigation and footer. Layouts let you write that frame once. This lesson explains _Layout.cshtml, RenderBody, sections, _ViewStart.cshtml and _ViewImports.cshtml.

How a page is assembled with a layout
  1. StartAction returns View()HomeController.Index asks for its view
  2. Step 2_ViewStart.cshtml runsSets Layout = "_Layout" for every view in the folder
  3. Step 3The view rendersIndex.cshtml produces the page body and any sections
  4. Step 4The layout renders_Layout.cshtml outputs the header, nav and footer
  5. Step 5RenderBody inserts the viewThe view output appears in the middle of the layout
  6. EndOne HTML page is sentShared frame plus page-specific content

The idea in plain words

A layout is a Razor file that contains the HTML shared by many pages. It calls RenderBody() at the spot where each page's own content should go. Views do not repeat the html, head or body tags; they only contain what is unique to them.

Two special files keep views short. _ViewStart.cshtml runs before every view in its folder and subfolders, and usually sets the layout. _ViewImports.cshtml holds @using directives and @addTagHelper lines that every view in the folder needs.

Example: a layout with a section

The layout renders the body and an optional Scripts section. ViewData["Title"], set by each view, fills the title tag.

Razor
@* Views/Shared/_Layout.cshtml *@
<!DOCTYPE html>
<html lang="en">
<head>
    <title>@ViewData["Title"] - My Shop</title>
    <link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
    <header><a asp-controller="Home" asp-action="Index">My Shop</a></header>
    <main>@RenderBody()</main>
    <footer>&copy; @DateTime.Now.Year My Shop</footer>
    @await RenderSectionAsync("Scripts", required: false)
</body>
</html>

The view and the helper files

A view fills the body and can add content to a named section. _ViewStart and _ViewImports apply automatically.

Razor
@* Views/_ViewStart.cshtml *@
@{ Layout = "_Layout"; }

@* Views/_ViewImports.cshtml *@
@using MyShop.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@* Views/Home/Index.cshtml *@
@{ ViewData["Title"] = "Home"; }
<h1>Welcome</h1>
@section Scripts {
    <script src="~/js/home.js"></script>
}

Common mistakes

Layout problems are usually about sections or file placement.

  • Defining a section in a view that the layout never renders, which throws an error.
  • Marking a section required: true and forgetting it in some views.
  • Putting @addTagHelper in a single view instead of _ViewImports, so Tag Helpers work in one page only.
  • Setting Layout = null by accident in a view and losing the whole site frame.

Interview question

What is the difference between _ViewStart.cshtml and _ViewImports.cshtml?

_ViewStart.cshtml contains code that runs before each view in its folder hierarchy, typically setting the Layout property. _ViewImports.cshtml does not run code; it provides directives shared by all views in the hierarchy, such as @using, @inject, @model defaults and @addTagHelper, so they do not have to be repeated in every view.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.