Examples

See the rendering model in practical situations.

Overview

Examples are useful only if they reflect the real rendering model of the framework. In PageBuilder, this means showing both visible HTML generation and the way components can contribute to the final document through the shared page model.

Example 1 — Layout using body capture

This is the core PageBuilder layout pattern. The body is rendered first into a temporary writer, allowing components to register page-level dependencies while rendering.

Body capture layout
@using DMBPageBuilder
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@{
    var originalWriter = ViewContext.Writer;
    var bodyWriter = new StringWriter();

    try
    {
        ViewContext.Writer = bodyWriter;
        RenderBody().WriteTo(ViewContext.Writer, HtmlEncoder);
    }
    finally
    {
        ViewContext.Writer = originalWriter;
    }

    var bodyHtml = bodyWriter.ToString();
    PageInformation page = PageRegistry.GetPageInformation(Context) ?? new PageInformation();
    PageBuilder builder = Html.PageBuilder(page);
}

@builder.RenderDocumentStart()
@Html.Raw(bodyHtml)
@builder.RenderDocumentEnd()

Example 2 — A body component registering a script

A component rendered in the body may require a script file. During rendering, it can register that dependency into PageInformation . The final document head or end-of-body section can then include it correctly. This is one of the reasons the body-first capture model exists.

Example 3 — Reusable builder rendered in Razor

A builder can encapsulate repeated rendering logic while still participating in the shared page model and lifecycle. Razor remains the composition layer, while the builder captures the reusable rendering behavior.

Example 4 — Builder-based rendering in C#

PageBuilder can be used directly from C# classes to generate structured HTML outside of Razor templates. This is especially useful for framework-level rendering, reusable infrastructure, and documentation generation.

What examples should demonstrate

The document pipeline
The body capture model
The shared page model through PageInformation
Reusable builder-based HTML generation
Separation between page orchestration and component rendering

Explore further

Go back to the Razor integration details, or explore the C# rendering model.

Using in Razor → Using in C# →