# Layouts and \_ViewStart

A layout holds the frame every page shares (the head, the menu, the footer) and @RenderBody() marks where each view goes. \_ViewStart picks the layout, and \_ViewImports holds what every view needs.

Apprentice · ASP.NET Core · Razor · Updated November 15, 2026

## After this lesson you can

- Share a frame across every page with a layout and @RenderBody()
- Explain what \_ViewStart.cshtml and \_ViewImports.cshtml do

## Key takeaways

- The layout, `Views/Shared/_Layout.cshtml`, holds the frame every page shares, and `@RenderBody()` marks where each view's HTML goes.
- To add something to every page, such as a menu link, add it to the layout once.
- A view's `ViewData["Title"]` reaches the layout, which puts it in the browser tab's title.
- `_ViewStart.cshtml` runs before every view and picks the layout; `_ViewImports.cshtml` holds the `@using` and `@addTagHelper` lines every view needs.

Every page of a website shares most of its HTML: the `<head>` with its CSS, the menu, the footer. Copying all that into every view would mean changing every view to add one menu link. Instead, ASP.NET Core keeps the shared frame in one file, the **layout**, and each view supplies only its own middle.

## \_Layout.cshtml

The template's layout is `Views/Shared/_Layout.cshtml`. Shortened, it looks like this:

```cshtml
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - NorthwindWeb</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
</head>
<body>
    <header>
        <nav class="navbar navbar-expand-sm navbar-light bg-white border-bottom mb-3">
            <div class="container-fluid">
                <a class="navbar-brand" asp-controller="Home" asp-action="Index">NorthwindWeb</a>
                <ul class="navbar-nav flex-grow-1">
                    <li class="nav-item">
                        <a class="nav-link text-dark" asp-controller="Home" asp-action="Index">Home</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link text-dark" asp-controller="Home" asp-action="Privacy">Privacy</a>
                    </li>
                </ul>
            </div>
        </nav>
    </header>
    <div class="container">
        <main role="main" class="pb-3">
            @RenderBody()
        </main>
    </div>

    <footer class="border-top footer text-muted">
        <div class="container">
            &copy; 2026 - NorthwindWeb
        </div>
    </footer>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    @await RenderSectionAsync("Scripts", required: false)
</body>
</html>
```

The parts to know:

| Part | What it does |
|---|---|
| `@RenderBody()` | Where the view's own HTML goes. A layout has exactly one. |
| `@ViewData["Title"]` | The browser tab's title, set by each view in its `@{ }` block |
| `~/` | The root of `wwwroot`: `~/css/site.css` is `wwwroot/css/site.css` |
| `@await RenderSectionAsync("Scripts", required: false)` | An optional slot where a view can add its own scripts |

It's a Bootstrap page, so the menu is a Bootstrap navbar, built with the tag helpers from the last lesson.

## Each view fills the middle

When `/Products` runs, ASP.NET Core runs `Views/Products/Index.cshtml` first, then puts its HTML where the layout says `@RenderBody()`. The view's `ViewData["Title"] = "Products";` reaches the layout too, so the tab reads **Products - NorthwindWeb**.

To add a link to every page, add it to the layout once. Give NorthwindWeb a **Products** link, after **Home**:

```cshtml
<li class="nav-item">
    <a class="nav-link text-dark" asp-controller="Products" asp-action="Index">Products</a>
</li>
```

## Sections

A **section** lets a view put something in a second place in the layout. A view that needs its own script writes it in a `Scripts` section:

```cshtml
@section Scripts {
    <script>
        console.log("Only the products page runs this.");
    </script>
}
```

The layout's `RenderSectionAsync("Scripts", required: false)` puts it near the end of the page, after Bootstrap's script. `required: false` means views without a `Scripts` section are fine.

## \_ViewStart.cshtml: which layout

How does every view know to use `_Layout`? `Views/_ViewStart.cshtml` runs before each view, and sets it:

```cshtml
@{
    Layout = "_Layout";
}
```

A view can change that for itself in its own code block: `Layout = null;` gives a page with no layout at all, for example.

## \_ViewImports.cshtml: what every view needs

`Views/_ViewImports.cshtml` holds the lines every view would otherwise start with:

```cshtml
@using NorthwindWeb
@using NorthwindWeb.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
```

The `@using` lines are why views can say `Product` instead of `NorthwindWeb.Models.Product`, and `@addTagHelper` turns on the tag helpers. When you add a folder of classes your views use, add its `@using` here.

| File | Runs or applies | Holds |
|---|---|---|
| `Views/Shared/_Layout.cshtml` | Around every view | The page frame, with `@RenderBody()` |
| `Views/_ViewStart.cshtml` | Before every view | Which layout to use |
| `Views/_ViewImports.cshtml` | To every view | `@using` and `@addTagHelper` lines |

The underscore at the start of their names is a convention: these files aren't pages of their own, and nobody browses to them.

## Do it: put a view inside the layout

When `/Products` runs, ASP.NET Core puts the view's HTML where the layout says `@RenderBody()`, and the view's title where it says `@ViewData["Title"]`. Write a method that does those two swaps.

The method gets three values:

- `layout`: the layout's HTML, a `string?`. It holds `@ViewData["Title"]` where the title goes and `@RenderBody()` where the view goes. It's `null` for a view that sets `Layout = null;`.
- `title`: the view's title, the `ViewData["Title"]` it set, a `string`, such as `"Products"`.
- `body`: the view's own HTML, a `string`, such as `"<h1>Products</h1>"`.

The box has a short layout for NorthwindWeb in `siteLayout`:

```csharp
string siteLayout = "<title>@ViewData[\"Title\"] - NorthwindWeb</title><main>@RenderBody()</main>";
```

For example, with `siteLayout`, the title `"Products"` and the body `"<h1>Products</h1>"`:

| Step | Swap | The page so far |
|---|---|---|
| 1 | `@ViewData["Title"]` becomes `Products` | `<title>Products - NorthwindWeb</title><main>@RenderBody()</main>` |
| 2 | `@RenderBody()` becomes `<h1>Products</h1>` | `<title>Products - NorthwindWeb</title><main><h1>Products</h1></main>` |

So `Render(siteLayout, "Products", "<h1>Products</h1>")` returns `"<title>Products - NorthwindWeb</title><main><h1>Products</h1></main>"`.

**Your job:** write the method `Render(string? layout, string title, string body)` so it returns the whole page, a `string`.

- Put the title where the layout says `@ViewData["Title"]`.
- Put the body where the layout says `@RenderBody()`.
- Leave the rest of the layout as it is.
- When `layout` is `null`, return the body alone.

Practiced live on the lesson page: write the C# right there and check it.

## Check yourself

2 questions to try on the lesson page.

## So what

One layout gives every page the same head, menu and footer, and changing the site's look means changing one file. Your views stay short: just the part that's different.

## Key terms

- Layout
- \_Layout.cshtml
- RenderBody
- Section
- \_ViewStart.cshtml
- \_ViewImports.cshtml

## Cheat sheets

- [Layouts and \_ViewStart](https://www.ask-codey.com/cheat-sheets/razor-views-layouts-and-viewstart-cheat-sheet)

Source: https://www.ask-codey.com/lessons/razor-views-layouts-and-viewstart
