RenderBody is the Razor layout method that marks where a child view’s body content appears. A layout typically places it inside shared HTML such as the document body, alongside common headers, footers, styles, and scripts. section 17.4.1
A basic layout
In _Layout.cshtml, @RenderBody() supplies the page-specific markup: <html> <head> <title>@ViewData["Title"]</title> <link rel="stylesheet" href="~/css/site.css" /> </head> <body> @RenderBody() </body> </html> A view can select this layout with Layout = "_Layout". Its heading and paragraph then appear at the RenderBody location, while ViewData can provide the page title. section 17.4.1
Use RenderBody with these layout rules:
- Call RenderBody in every layout that renders a child view body; otherwise Razor throws an InvalidOperationException unless the body is explicitly ignored. section 17.4.2
- Use sections when one body location is too restrictive, such as a sidebar or optional scripts area. section 17.4.1 section 17.4.2
- Nest layouts when different groups of views share additional structure beyond the base layout. section 17.4.2
Listing 17.7 A basic _Layout.cshtml file calling RenderBody
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>@ViewData["Title"]</title> #1
<link rel="stylesheet" href="~/css/site.css" /> #2
</head>
<body>
@RenderBody() #3
</body>
</html>
#1 ViewData is the standard mechanism for passing data to a layout from a view.
#2 Elements common to every page, such as your CSS, are typically found in the layout.
#3 Tells the templating engine where to insert the child view’s content
Inspect the complete _Layout.cshtml example showing shared document markup, ViewData, a stylesheet, and the RenderBody call. section 17.4.1

