A powerful library for rendering Razor components as HTML strings or IResult responses in ASP.NET Core minimal APIs. Features fluent HTML builders for tables, selects, and dynamic content.
Build HTML programmatically with a fluent, type-safe API designed for minimal APIs and server-side rendering.
Chain methods to build complex HTML structures with IntelliSense support and compile-time safety.
Generate tables, selects, and lists from collections with automatic column definitions and grouping.
Return HTML directly from endpoints with Results.Razor() or render components to strings.
Install RazorHelpers and start building HTML with a fluent, type-safe API.
Add to Program.cs
using RazorHelpers;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorHelpers();
var app = builder.Build();
// Return HTML from minimal API endpoints
app.MapGet("/", () => RazorResults.Razor(
Html.Div()
.Class("container")
.Child(Html.H1("Welcome!"))
.Child(Html.P("Built with RazorHelpers"))
));
// Generate tables from collections
app.MapGet("/users", () => RazorResults.Razor(
Html.Table(users)
.Class("table")
.Column("Name", u => u.Name)
.Column("Email", u => u.Email)
));
app.Run();
Comprehensive builders for every HTML element and collection type.
The Html class provides static methods for creating all HTML elements with a fluent API.
// Build a card component
var card = Html.Div()
.Class("card", "shadow-lg")
.Id("user-card")
.Child(Html.Img("/avatar.jpg", "Avatar")
.Class("rounded-full"))
.Child(Html.H2("John Doe"))
.Child(Html.P("Software Engineer")
.Class("text-gray-500"))
.Child(Html.A("mailto:john@example.com", "Email")
.Class("btn", "btn-primary"));
// Conditional classes
var btn = Html.Button("Submit")
.Class("btn")
.ClassIf("btn-disabled", isDisabled)
.Attr("disabled", isDisabled);
// Inline styles
var alert = Html.Div("Success!")
.Style("color", "green")
.Style("padding", "1rem");
.Class() - CSS classes.ClassIf() - Conditional class.Id() - Element ID.Attr() - Any attribute.Attrs() - Multiple attributes.Data() - Data attributes.Style() - Single style.Styles() - Multiple styles.Text() - Encoded text.Raw() - Raw HTML.Child() - Add child.Children() - Multiple children.Content() - RenderFragmentGenerate HTML tables from collections with automatic column definitions, row styling, and custom cell rendering.
// Simple table from collection
var table = Html.Table(users)
.Class("table", "table-striped")
.Column("Name", u => u.Name)
.Column("Email", u => u.Email)
.Column("Status", u => u.IsActive ? "Active" : "Inactive");
// With custom cell rendering
var table = Html.Table(users)
.Column("Name", u => Html.Strong(u.Name))
.Column("Email", u =>
Html.A($"mailto:{u.Email}", u.Email))
.Column("Status", u =>
Html.Span(u.IsActive ? "Active" : "Inactive")
.Class(u.IsActive ? "text-green-500" : "text-red-500"));
// Conditional row styling
var table = Html.Table(orders)
.Column("Order #", o => o.Id.ToString())
.Column("Total", o => o.Total.ToString("C"))
.RowClass(o => o.IsPending ? "bg-yellow-50" : "");
Create dropdown selects from collections with support for grouping, placeholders, and conditional selection.
// Simple select from collection
var select = Html.Select(countries, "country")
.Value(c => c.Code)
.Text(c => c.Name)
.Placeholder("Select a country...")
.SelectedValue("us");
// Grouped select (optgroups)
var carSelect = Html.Select(cars, "car")
.Value(c => c.Id)
.Text(c => c.Model)
.GroupBy(c => c.Manufacturer);
// Renders:
// <optgroup label="Toyota">
// <option value="1">Camry</option>
// </optgroup>
// With disabled options
var planSelect = Html.Select(plans, "plan")
.Value(p => p.Id)
.Text(p => p.Name)
.DisabledOption(p => p.RequiresPremium);
Multiple ways to render HTML elements as strings or IResult responses.
Returns IResult for minimal API endpoints.
Renders RenderFragment to HTML string asynchronously.
Renders Razor components to strings with parameters.
// Minimal API endpoint
app.MapGet("/page", () =>
RazorResults.Razor(Html.Div("Hello")));
// With model binding
app.MapGet("/user/{id}", (int id) =>
RazorResults.Razor(template, new { Id = id }));
// Render to string
var html = await Html.Div("Content")
.Render()
.RenderAsync(services);
// Render component with parameters
var html = await ComponentHelper
.RenderComponentAsync<MyComponent>(
services,
new Dictionary<string, object?>
{
["Title"] = "Hello"
});
Build lists and repeat elements from collections with Html.Ul(), Html.Ol(), and Html.Each().
// Simple list from strings
var ul = Html.Ul(items, x => x);
// List with custom item rendering
var ul = Html.Ul(products, p =>
Html.A($"/product/{p.Id}", p.Name));
// Ordered list
var ol = Html.Ol(steps, s => s.Title);
// Repeat elements for each item
var cards = Html.Each(products, p =>
Html.Div()
.Class("card")
.Child(Html.H3(p.Name))
.Child(Html.P(p.Description)));
// With index
var rows = Html.Each(items, (item, i) =>
Html.Div($"{i + 1}. {item.Name}"));
All common HTML text formatting elements are available with the fluent API.
Install RazorHelpers and start building HTML with a fluent, type-safe API today.