Request

The HtmxRequest class provides properties to determine characteristics of a request triggered by HTMX within an ASP.NET application. This class helps in identifying whether a request is made via HTMX, if it is boosted, part of a history restore, or contains specific HTMX-related headers such as the current URL, target, trigger name, trigger id, and prompt responses.

PropertyTypeDescription
MethodstringGets the HTTP method of the current request.
PathPathStringGets the request path.
IsHtmxboolGets whether or not the current request is an Htmx triggered request.
IsBoostedboolGets whether or not the current request is initiated via an element using hx-boost.
IsHistoryRestoreboolGets whether or not the current request is an Htmx history restore request.
CurrentURLUri?Gets the current URL of the browser.
Targetstring?Gets the id of the target element if it exists.
TriggerNamestring?Gets the name of the triggered element if it exists.
Triggerstring?Gets the id of the triggered element if it exists.
Promptstring?Gets the user response to an hx-prompt, if any.

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 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 HtmxResponse object is accessible from the Response controller property:

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;

Target

Retrieves the id of the target element from the HTMX request if it exists.

Example

string? targetId = Request.Htmx().Target;

TriggerName

Obtains the name of the element that triggered the HTMX request if available.

Example

string? triggerName = Request.Htmx().TriggerName;

Trigger

Gets the id of the element that initiated the HTMX request if present.

Example

string? triggerId = Request.Htmx().Trigger;

Prompt

Retrieves the user response to an hx-prompt from the HTMX request, if any.

Example

string? promptResponse = Request.Htmx().Prompt;