Using in Razor (cshtml)

Use Razor for composition, and PageBuilder for rendering discipline.

Overview

PageBuilder integrates naturally into Razor views. It does not replace Razor. Instead, it gives Razor a structured rendering model based on builders, a shared page model, and a consistent document pipeline.

The most important thing to understand is that a Razor page using PageBuilder is not limited to writing visible HTML. During rendering, components can also contribute to the final document by registering scripts, stylesheets, inline assets, and page-level configuration.

Razor and the shared page model

When a component is rendered inside the body, it may need to declare page-level dependencies. For example, a component may require:

A JavaScript file
A stylesheet
Inline JavaScript
Inline CSS
Page metadata or configuration
Body-first rendering

These dependencies are typically registered through PageInformation while the body is rendering. This is why the body is often captured first in the layout before the final document shell is emitted.

Typical layout pattern

In a Razor layout, the common pattern is to render the body into a temporary writer, restore the original writer, then render the final document using the now-complete PageInformation .

Typical Razor 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()

This pattern allows components rendered in the body to enrich the shared page model before the final <head> section is written.

Using builders inside a Razor view

Inside a Razor view, PageBuilder builders can be used to generate reusable, structured HTML. Razor remains the place where page intent is expressed, while builders encapsulate rendering rules and reusable structure.

Use Razor to compose the page
Use PageBuilder to structure HTML generation
Use builders when markup logic becomes repeated or stateful

Why this works well in Razor

Razor remains readable and familiar
Rendering logic becomes reusable
Page-level dependencies can still be registered during body rendering
The final document remains centrally orchestrated

PageBuilder does not fight Razor. It gives Razor a stronger rendering discipline.

Recommended usage

Use the layout to orchestrate body capture and document rendering
Keep page-level asset registration inside builders and components
Use builders for repeated or structural rendering logic
Keep simple page intent readable in Razor

Continue with Using in C#

See how builders can be used outside views for reusable infrastructure.

Using in C# →