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
requestHttpRequestThe HTTP request.
Returns
IsHtmxBoosted(HttpRequest)
Determines whether the request came from a link or form enhanced with hx-boost.
public static bool IsHtmxBoosted(this HttpRequest request)
Parameters
requestHttpRequestThe HTTP request.
Returns
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
requestHttpRequestThe HTTP request.
Returns
- bool
true if the
HX-Request-Typeheader 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.9.x and 2.x, the Toolkit script adds it. See RequestType.
IsHtmxPartialRequest(HttpRequest)
Determines whether the specified HTTP request expects an HTML fragment.
public static bool IsHtmxPartialRequest(this HttpRequest request)
Parameters
requestHttpRequestThe HTTP request.
Returns
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.9.x and 2.x, the Toolkit script 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
requestHttpRequestThe HTTP request.
Returns
- bool
true if the request has the
HX-Requestheader and is not a history restoration request; otherwise, false.
Remarks
HTMX treats history restoration as a full page load rather than an HTMX request.
HTMX 4.x sends it without HX-Request, but HTMX 1.9.x and 2.x send that header by default,
so the method also checks HistoryRestoreRequest.
See issue #4139.
Request reports the HX-Request header alone.
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
requestHttpRequestThe HTTP request.
headersHtmxRequestHeadersWhen this method returns, contains a strongly typed view of the HTMX request headers. The value is set even when the method returns false.
Returns
- bool
true if the request has the
HX-Requestheader and is not a history restoration request; otherwise, false.
Examples
if (Request.IsHtmxRequest(out var htmx))
{
logger.LogDebug(
"HTMX request for {Target} from {Source}",
htmx.Target,
htmx.Source ?? htmx.Trigger);
}