- Home
- /
- Foundation
- /
- DMBPageBuilder
- /
- Using in Razor (cshtml)
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:
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 .
@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.
Why this works well in Razor
PageBuilder does not fight Razor. It gives Razor a stronger rendering discipline.
Recommended usage
Continue with Using in C#
See how builders can be used outside views for reusable infrastructure.
Using in C# →