Getting Started

This guide sets up Rizzy in an ASP.NET Core app and renders a Razor component that htmx updates without a page reload. Upgrading an existing Rizzy 5 app? See Upgrading to Rizzy 6.

Want a ready-made app with UI components?

dotnet new install RizzyUI.Templates, then dotnet new rizzyui -o MyApp, creates a Rizzy 6 app with RizzyUI components, a sidebar layout, a validated htmx form and Tailwind CSS already set up. See RizzyUI 1.0.0 Preview 4. This guide shows what such an app is built from.

Requirements

  • .NET 10 or .NET 11. The packages include a build for each. On .NET 11, Rizzy also protects posted forms without antiforgery tokens, runs async validators, and supports CacheView (see .NET 11 below).
  • htmx 4.0.1 or newer, loaded by your layout (from a CDN or your own static files).

Installation

dotnet add package Rizzy --prerelease

Rizzy 6 is in preview

Until 6.0.0 is released, include --prerelease (in Visual Studio, tick Include prerelease). Without it you get Rizzy 5.x, which doesn’t support htmx 4.

The Rizzy package brings the matching Rizzy.Htmx package and the Rizzy client script (_content/Rizzy/js/rizzy.js). The three are always released together with the same version.

Register Rizzy (Program.cs)

Builder Services
Program.cs
using Rizzy;
using MyApp; // the namespace of your components (your project's root namespace)

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews(); // only if you use MVC controllers
builder.Services.AddRizzy(options =>
{
    options.RootComponent = typeof(HtmxApp<AppLayout>);    // the HTML document
    options.DefaultLayout = typeof(HtmxLayout<MainLayout>); // page chrome, skipped for htmx fragments
});

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();   // serves /_content/Rizzy/js/rizzy.js
app.UseRouting();
// app.UseAuthentication(); app.UseAuthorization(); if you use them
app.UseAntiforgery();
app.UseRizzy();

app.MapGet("/", (IRizzyService rizzy) => rizzy.View<Home>());
app.MapPost("/greet", (IRizzyService rizzy) => rizzy.PartialView<Greeting>(new { Name = "htmx" }));
app.MapDefaultControllerRoute();

app.Run();

What this does:

  • AddRizzy(...) registers everything Rizzy needs, including htmx support (AddHtmx) and Razor components (AddRazorComponents). Repeated calls are safe. Call AddHtmx(config => ...) as well only to change the htmx configuration.
  • RootComponent is the component that renders the HTML document. HtmxApp<AppLayout> renders your AppLayout for normal requests and skips it for htmx fragment requests. Startup fails if it is null, and a warning is logged if you leave the default (which renders no <head>).
  • DefaultLayout applies to components without an @layout directive. HtmxLayout<MainLayout> renders MainLayout for full pages, boosted navigation and back/forward, and skips it for htmx requests that target one element. See Layouts.
  • AddControllersWithViews() (rather than AddControllers()) is needed if your MVC actions use [ValidateAntiForgeryToken]; it registers the MVC antiforgery filters.
  • UseAntiforgery() goes after routing and authentication. UseRizzy() goes after it.

Add _Imports.razor

_Imports.razor
@using Microsoft.AspNetCore.Components.Web
@using Rizzy

Create the root layout

The root layout renders <RzHeadOutlet /> and then <RzScripts /> in its <head>. RzHeadOutlet renders Blazor’s HeadOutlet (page titles and <HeadContent>) plus Rizzy’s <meta name="htmx-config"> tag, which carries your htmx settings and the antiforgery token. RzScripts then loads htmx (the tested version, with a subresource integrity hash), the Rizzy script and any extensions you enable. htmx reads the meta tag once, when its script loads, so the outlet comes first.

AppLayout.razor
@inherits LayoutComponentBase

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <base href="/" />
    <RzHeadOutlet />
    <RzScripts />
</head>
<body>
    @Body
</body>
</html>
MainLayout.razor
@inherits LayoutComponentBase

<main>@Body</main>

The Rizzy script registers Rizzy’s own htmx extensions (antiforgery, CSP nonces, streaming rendering, confirm dialogs) when it loads; no hx-ext attributes are needed. To load the scripts yourself instead, see HtmxConfig.Scripts on the Extensions page.

Write components

Home.razor
<RzPageTitle>Home</RzPageTitle>

<h1>Hello</h1>
<button hx-post="/greet" hx-target="#greeting">Greet</button>
<div id="greeting"></div>
Greeting.razor
<p>Hello, @Name!</p>

@code {
    [Parameter] public string? Name { get; set; }
}

Plain hx-* attributes work, and so does the typed Hx builder: <button @attributes="@(Hx.Post("/greet").Target("#greeting"))">Greet</button>.

Run the app and click the button. htmx posts to /greet (rizzy.js adds the antiforgery token), and PartialView<Greeting> returns just the fragment, which htmx swaps into #greeting. View<Home> renders the full page for a normal request, but only the component for an htmx request that targets an element.

Keep the console open

htmx 4 logs every error to the browser console, and Rizzy logs one clear warning when the page is misconfigured (for example a root layout without <RzHeadOutlet />). See Troubleshooting.

Next steps

  1. Use MVC controllers: inherit from RzController (or RzControllerWithViews) and return View<T>()/PartialView<T>(). See Setup with MVC Controllers.
  2. Build forms with server-side validation: Validation.
  3. Send extra fragments in one response: Out of Band Swapping.
  4. Control htmx from the server: Response.
  5. Lock down scripts: Content Security Policy.
  6. Add UI components: RizzyUI, or start a new app from its template with dotnet new rizzyui.

The src/RizzyDemo app in the Rizzy repository shows all of these together, including streaming rendering, 422 validation and a nonce-based CSP.

.NET 11

Rizzy works the same on .NET 10 and .NET 11. On .NET 11 you also get:

  • Form posts protected without tokens. When Rizzy supplies a posted form to [SupplyParameterFromForm], it checks the request with ASP.NET Core 11’s automatic CSRF protection, which uses the browser’s Sec-Fetch-Site and Origin headers. Same-origin posts (every htmx request) pass without a token, and cross-site posts get 400 Bad Request. The antiforgery token setup above keeps working and is still the default.
  • Async validation. RzInitialValidator runs async validators (AsyncValidationAttribute, IAsyncValidatableObject), for checks such as “is this username taken?”.
  • CacheView. ASP.NET Core 11’s <CacheView> caches the HTML of part of a Rizzy view. Use VaryByHeader="HX-Request" so full pages and htmx partials are cached separately. Inside a CacheView, set the page title with <RzPageTitle Text="..." />.