Samples
The sample lives in this project’s GitHub repository, khalidabuhakmeh/Htmx.Net. Clone it, then run the app:
git clone https://github.com/khalidabuhakmeh/Htmx.Net.gitcd Htmx.Netdotnet run --project samples/Htmx4.Samplesamples/Htmx4.Sample/ is a Razor Pages + MVC app on htmx 4.0.0 (vendored in wwwroot/lib/htmx/).
Then open the demo you want (each is at /<Name>). The sample’s Demos.cs lists the pages and the layout renders the navigation from it. Each demo ends with a “How it works” panel showing its key markup and server code.
The 13 demos
Section titled “The 13 demos”| Demo | What it shows | Source | Guide |
|---|---|---|---|
/RequestHeaders |
Echoes every request header htmx sent: IsHtmx, Source/Target (parsed into HtmxElementReference), RequestType, CurrentUrl, Boosted, history restore, Prompt. |
RequestHeaders.cshtml | Request detection |
/ResponseHeaders |
One button per response header (PushUrl, PreventPush, ReplaceUrl, Redirect, Refresh, Retarget, Reswap, Reselect, Location) and every HtmxSwap style, with WithVary(). |
ResponseHeaders.cshtml | Response headers |
/Triggers |
WithTrigger variants: plain event, scalar detail, object detail, multiple events, merging with an existing HX-Trigger, first-wins. |
Triggers.cshtml | Triggering events |
/Config |
Page-scoped htmx-config via a layout section covering all config attributes, with htmx.config read back; default swap and noSwap in action. |
Config.cshtml | Config |
/UrlHelper |
HtmxUrlTagHelper: Razor Page handler, MVC controller/action, named route with route values, area, and POST. |
UrlHelper.cshtml | Url tag helper |
/Inherited |
Explicit htmx 4 inheritance: hx-target:inherited and hx-headers-* with hx-headers-inherited, compared to a non-inherited parent. |
Inherited.cshtml | Url tag helper |
/Antiforgery |
Token in header mode and form mode for POST/PUT/PATCH/DELETE, [AutoValidateAntiforgeryToken], [ValidateAntiForgeryToken], and HtmxSnippets / Html.HtmxAntiforgeryScript(). |
Antiforgery.cshtml | Antiforgery |
/Boosted |
hx-boost navigation between pages, IsHtmxBoosted() / IsHtmxNonBoosted() and a boosted POST. |
Boosted.cshtml | Request detection |
/Partials |
<hx-partial> multi-target response with OOB swap and main-content-first ordering. |
Partials.cshtml | Partials and morph |
/Morph |
outerMorph and innerMorph swaps that preserve typed input, compared with a plain swap. |
Morph.cshtml | Partials and morph |
/ErrorSwaps |
4xx/5xx swapping by default, 204/304 skipped, a validation POST, and hx-delete with hx-include. |
ErrorSwaps.cshtml | Partials and morph |
/Location |
HX-Location as a string and as HtmxLocation (target, swap, select, selectOOB, values, headers, transition). |
Location.cshtml | Response headers |
/Polling |
The htmx 4 polling pattern: load delay:1s and every 1s, stopped by replacing the poller (no StopPolling()). |
Polling.cshtml | Response headers |
Fragments are served by Razor Page handlers and by Controllers/DemoController.cs (routes under /demo/...), and every fragment carries data-testid hooks for the tests.
Supporting pieces
Section titled “Supporting pieces”Pages/Shared/_Layout.cshtml: globalhtmx-configmeta (replaceable by theHtmxConfigsection), vendored htmx with SRI, and the antiforgery script.Controllers/DemoController.cs: the MVC fragment endpoints, includingAntiforgeryDemoController.Controllers/AntiforgeryTestController.cs: test-only endpoints that simulate a hostile second origin. Not recommended practice.Areas/Admin/Controllers/AdminController.cs: the area target used by the URL helper demo.
Proof it works
Section titled “Proof it works”samples/Htmx4.Sample/README.mdhas the full API to demo-page checklist for both packages.test/Htmx.IntegrationTestsexercises the sample over HTTP (WebApplicationFactory).test/Htmx.E2ETestsdrives the sample in a real browser with Playwright (Category=E2E), including the antiforgery cross-origin,<base href>and redirect scenarios.test/Htmx.Testsholds unit and snapshot tests.
New here? Start with Getting started.
More recipes on the blog
Section titled “More recipes on the blog”Worked examples that combine htmx.NET with other libraries and patterns: