Request

The HtmxRequest class reads the htmx 4 request headers: whether a request was made by htmx, whether it is boosted or a history restore, what kind of response it expects, and which elements are involved.

PropertyTypeDescription
MethodstringThe HTTP method of the current request.
PathPathStringThe request path.
IsHtmxboolWhether the request was made by htmx (HX-Request).
IsBoostedboolWhether the request came from an element using hx-boost.
IsHistoryRestoreboolWhether htmx is restoring a page on back/forward (HX-History-Restore-Request).
CurrentURLUri?The browser’s current URL (HX-Current-URL).
RequestTypestring?The raw HX-Request-Type header: partial or full.
TypeHtmxRequestTypeThe parsed request type: None, Partial, Full or Unknown.
IsPartialboolWhether the response should be a fragment: an htmx request that targets part of the page and is not a history restore.
IsFullPageboolThe inverse of IsPartial: a full page is expected.
Targetstring?The raw HX-Target header, in htmx 4’s tag#id form (for example div#results).
TargetIdstring?The id part of Target (URI-decoded), or null when the target has no id (HX-Target: div).
Sourcestring?The raw HX-Source header for the element that triggered the request, in tag#id form.
SourceIdstring?The id part of Source, or null when the element has no id.
Promptstring?The answer to hx-prompt (HX-Prompt, URI-decoded). Needs the hx-prompt extension.
PTagstring?The element’s polling tag (HX-PTag). Needs the hx-ptag extension; answer 304 when nothing changed.
LastPartIdstring?The last part id before a reconnect (HX-Last-Part-ID). Needs the hx-multipart extension.

HtmxLayout uses IsPartial to decide whether to render your layout; see Layouts.

Rizzy 6

htmx 4 no longer sends HX-Trigger, HX-Trigger-Name or HX-Prompt, so Rizzy 6 removed Trigger, TriggerName and Prompt. Use Source/SourceId for the element that issued the request.

Note

The HtmxRequest class was constructed based on HTMX usage of Request headers. You can find more information about HTMX request headers on the official htmx 4 documentation site.

Working With HtmxRequest

HtmxRequest only has a dependency on HttpContext, so you can use it in any scoped dependency service that has access to HttpContext. To manually create an HtmxRequest object you need only to create a new instance of the class as follows:

var request = new HtmxRequest(httpContext);

The HtmxRequest object is accessible from the Request property of a controller (or any HttpRequest):

var request = Request.Htmx();

IsHtmx

Determines if the current request was triggered by HTMX.

Example

bool isHtmxRequest = Request.IsHtmx();

IsBoosted

Checks whether the current request was initiated via an element using hx-boost.

Example

bool isBoosted = Request.Htmx().IsBoosted;

IsHistoryRestore

Identifies if the request is an HTMX history restore request.

Example

bool isHistoryRestore = Request.Htmx().IsHistoryRestore;

CurrentURL

Gets the current URL from the browser as triggered by the HTMX request.

Example

Uri? currentUrl = Request.Htmx().CurrentURL;

IsPartial and Type

Tells you what kind of response htmx expects. Most code doesn’t need this, because View<T> already renders a fragment or a full page accordingly.

Example

var htmx = Request.Htmx();
if (htmx.IsPartial)
{
    // htmx targets one element: return just the fragment
}
else if (htmx.Type == HtmxRequestType.Full)
{
    // boosted body swap, hx-select or history restore: return the full page
}

Target and TargetId

Target is the raw HX-Target header (tag#id in htmx 4); TargetId is the id, or null if the target element has no id.

Example

string? targetId = Request.Htmx().TargetId;   // "results" for HX-Target: div#results

Source and SourceId

The element that triggered the request.

Example

string? sourceId = Request.Htmx().SourceId;   // "save" for HX-Source: button#save