liveBook · Manning

RenderBody

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

RenderBody is a Razor layout method that inserts a view's content not assigned to a section into the rendered response.

RenderBody is the insertion point in a Razor layout for content unique to the current view. Shared markup stays in _Layout.cshtml; each view contributes the remaining, unsectioned markup at this call. section 22.4

The method is inherited from RazorPage<T>. A view selects the layout by assigning Layout = "_Layout"; the Razor engine searches the controller and Shared view folders for the matching file. section 22.4

Place view content inside shared markup

_Layout.cshtml can provide the document shell and call RenderBody where page-specific output belongs:\n\n<body>\n <h6>Shared View</h6>\n @RenderBody()\n</body>\n\nA view using Layout = "_Layout" can then contain only its own table or other page markup. Content declared with @section is different: RenderSection places that named fragment, while RenderBody receives everything else. section 22.4 section 22.4.4

Listing 22.9 The contents of the _Layout.cshtml file in the Views/Shared folder
<!DOCTYPE html>
<html>
<head>
   <link href="/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
   <h6 class="bg-primary text-white text-center m-2 p-2">Shared View</h6>
   @RenderBody()
</body>
</html>

Inspect the complete layout listing to see RenderBody positioned between shared HTML elements. section 22.4

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

How deep it goes
Explains inheritance, layout selection, and section boundaries, but does not descend into Razor rendering internals.
Examples
Uses _Layout.cshtml with shared HTML, a Product view, and table content inserted through RenderBody.
What it assumes
Assumes basic Razor views and HTML, while defining layout selection and RazorPage<T> inheritance.
What you can do after
Builds a shared Razor layout that renders unsectioned view content at a chosen location.
How it connects
Connects Razor layouts, RazorPage<T>, view sections, and Razor views.

Also explained in

Compare how 2 books explain RenderBody →

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.