What is a layout page in Razor, and what are sections?

ASP.NET MVC Views & Razor Basic Fresher1–2 Yrs Conceptual 1 min read

Quick Answer

A layout is a master template (_Layout.cshtml) that holds the shared HTML shell — head, header, footer — with @RenderBody() where the page's content goes. Sections (@RenderSection("Scripts", required: false)) are named placeholders a page can fill, e.g. page-specific scripts or a sidebar.

A layout is the shared shell — _Layout.cshtml — with the head, header and footer, and @RenderBody() marks where each page's content is injected. _ViewStart wires the default layout so I don't set it in every view. Sections are named slots: the layout declares @RenderSection("Scripts", required: false) and a specific view fills it with @section Scripts { ... } — that's how a page adds its own JavaScript without every page carrying it. _ViewImports is where shared usings and tag helper registrations live.

  • _ViewStart.cshtml sets the default Layout for a folder tree; a view can override Layout = null or point to another.
  • @RenderBody() — required, one per layout, renders the child view.
  • @RenderSection("name", required: false) in the layout; @section name { ... } in the view.
  • _ViewImports.cshtml holds shared @using, @inject and tag-helper registrations.
  • Layouts can nest — a section layout inside the site layout.