Free Web Development Online Tutorials, Learn to Code
Learn MVC C#, MVC Tutorial for Beginners, Model View Controller

RenderBody, RenderSection and RenderPage in Razor

Render Razor Page

How does Layouts RenderBody, RenderSection, RenderPage, Html.Partial work in ASP.NET MVC, Layouts are used to maintain a consistent look and feel across multiple views, Layouts is like master page.

Let’s see how the rendering life cycle works, we learn each function and its use.

Basic Layout Page structure
<!DOCTYPE html>           
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
@RenderSection("metatags", required: false)
@ Scripts.Render("~/bundles/modernizr")
@RenderSection("scripts", required: false)

RenderBody method exists in the Layout page to render child page/view. It is just like the ContentPlaceHolder in master page. A layout page can have only one RenderBody method.

@RenderSection("metatags", required: false)

We can have any number of @RenderSection("metatags") in a Layout, but key (ex. "metatags") has to be unique. "required: false" is means this section is optional for the view, that will consume this masterpage, Otherwise this section must be specified in the view.

Section implementation
@section metatags
<meta name="description" content="How Layouts, RenderBody, RenderSection,  RenderPage, Html.Partial works" /> 
Scripts.Render & Styles.Render

Scripts.Render is used to render a bundle of Script files by rendering script tag(s) for the Script bundle in BundleConfig

Style.Render is used to render a bundle of CSS files defined in BundleConfig.cs files.

let's look at code

public class BundleConfig            
    public static void RegisterBundles(BundleCollection bundles)
        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
        bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(

Scripts.Render & Styles.Render generate multiple script & style tags for each item in the Script bundle and CSS bundle when optimizations are disabled.

But, When optimizations are enabled, they generate a single style and script tag to a version-stamped URL which represents the entire bundle for Scripts & CSS


Html.Partial("") is just like calling user control (partial view) in main page


RenderPage("") is just like calling another page into main page, note: while calling you have to specify ".cshtml"

RenderBody, RenderSection, RenderPage in ASP.NET MVC