Welcome to Swap.Htmx
Swap.Htmx is an opinionated way to build interactive ASP.NET Core apps while keeping the web simple: controllers return HTML, HTMX swaps it in, and the server coordinates UI updates.
If you’ve ever felt like you had to pick between:
- “plain MVC” (simple, but harder to keep rich UIs consistent), or
- a full SPA (powerful, but adds build tools + client state + duplicated logic),
Swap.Htmx sits in the middle: server-driven UI with first-class ergonomics for partial updates.
What is Swap.Htmx?
Swap.Htmx is an open-source .NET library for building server-driven web apps using server-rendered HTML. It pairs naturally with HTMX (https://htmx.org) to bring interactivity to your pages without adopting a JavaScript framework.
At a high level:
- You return HTML from controllers (full pages or partials).
- HTMX handles the browser-side swapping.
- Swap.Htmx helps you coordinate multi-part UI updates, UI events, and server-side UI state.
The core idea: one action, multiple UI updates
In real apps, one user action often affects more than one piece of UI: a list, a counter, a toast message, a preview panel, etc.
Swap.Htmx makes it straightforward to respond to one request with multiple updates, without wiring up a client-side state store.
Here’s the mental model:
return SwapResponse()
.WithView("_ProductCard", product)
.AlsoUpdate("cart-count", "_CartCount", count)
.WithSuccessToast("Added to cart!")
.Build();
(That’s a conceptual example — the point is the shape of the response: multiple updates from one server action.)
What you get out of the box
A few highlights you’ll see throughout the docs:
SwapView(): return one result that works for both full navigations and HTMX requests.SwapResponse(): update multiple targets in a single response (including toasts and triggers).- Events / handlers: decouple “what happened” from “what parts of the UI need to update.”
SwapState: keep UI state (filters, pagination, wizards) on the server and persist it through hidden fields.- Type-safe helpers: source generators can reduce magic strings for views, element IDs, and event keys.
Who is this for?
Swap.Htmx is a great fit if you:
- build ASP.NET Core MVC/Razor apps,
- like server-rendered HTML,
- want rich interactivity without managing a lot of client-side state.
It’s not trying to replace every use of JavaScript. The goal is to remove the need for a full JavaScript framework for a large class of apps.