Table of Contents

Class HttpRequestExtensions

Namespace
Ramstack.HtmxToolkit
Assembly
Ramstack.HtmxToolkit.dll

Provides extension methods for the HttpRequest class.

public static class HttpRequestExtensions
Inheritance
HttpRequestExtensions
Inherited Members

Methods

GetHtmxHeaders(HttpRequest)

Returns a strongly typed view of the HTMX request headers.

public static HtmxRequestHeaders GetHtmxHeaders(this HttpRequest request)

Parameters

request HttpRequest

The HTTP request.

Returns

HtmxRequestHeaders

The HtmxRequestHeaders.

IsHtmxBoosted(HttpRequest)

Determines whether the request came from a link or form enhanced with hx-boost.

public static bool IsHtmxBoosted(this HttpRequest request)

Parameters

request HttpRequest

The HTTP request.

Returns

bool

true if the specified HTTP request is boosted; otherwise, false.

Examples

public IActionResult Navigation()
{
    if (Request.IsHtmxBoosted())
        return PartialView("_Navigation", menu.Items);

    return RedirectToAction("Index");
}

IsHtmxFullRequest(HttpRequest)

Determines whether the specified HTTP request expects a full page.

public static bool IsHtmxFullRequest(this HttpRequest request)

Parameters

request HttpRequest

The HTTP request.

Returns

bool

true if the HX-Request-Type header value is "full", or the header is absent and the request restores history; otherwise, false.

Remarks

HTMX 4.x sends the header itself; for HTMX 1.x and 2.x, the companion script htmx-toolkit.js adds it. See RequestType.

IsHtmxPartialRequest(HttpRequest)

Determines whether the specified HTTP request expects an HTML fragment.

public static bool IsHtmxPartialRequest(this HttpRequest request)

Parameters

request HttpRequest

The HTTP request.

Returns

bool

true if the HX-Request-Type header value is "partial"; otherwise, false.

Examples

public IActionResult Details(int id)
{
    var product = catalog.Get(id);

    if (Request.IsHtmxPartialRequest())
        return PartialView("_ProductDetails", product);

    return View(product);
}

Remarks

HTMX 4.x sends the header itself; for HTMX 1.x and 2.x, the companion script htmx-toolkit.js adds it. Without the header, the method returns false.

IsHtmxRequest(HttpRequest)

Determines whether the specified HTTP request is an HTMX request.

public static bool IsHtmxRequest(this HttpRequest request)

Parameters

request HttpRequest

The HTTP request.

Returns

bool

true if the specified HTTP request is an HTMX request; otherwise, false.

IsHtmxRequest(HttpRequest, out HtmxRequestHeaders)

Determines whether the specified HTTP request is an HTMX request.

public static bool IsHtmxRequest(this HttpRequest request, out HtmxRequestHeaders headers)

Parameters

request HttpRequest

The HTTP request.

headers HtmxRequestHeaders

When this method returns, contains the HtmxRequestHeaders that provides access to well-known HTMX headers.

Returns

bool

true if the specified HTTP request is an HTMX request; otherwise, false.

Examples

if (Request.IsHtmxRequest(out var htmx))
{
    logger.LogDebug(
        "HTMX request for {Target} from {Source}",
        htmx.Target,
        htmx.Source ?? htmx.Trigger);
}