Table of Contents

Struct HtmxResponse

Namespace
Ramstack.HtmxToolkit
Assembly
Ramstack.HtmxToolkit.dll

Represents an HTTP response whose HTMX response headers can be configured.

public readonly struct HtmxResponse
Inherited Members

Remarks

Like HttpContext and HttpResponse themselves, this type is not thread-safe. Its members should not be called concurrently from multiple threads for the same request.

Properties

Headers

Gets the strongly typed HTMX response headers.

public HtmxResponseHeaders Headers { get; }

Property Value

HtmxResponseHeaders

Methods

Location(string)

Sets the HX-Location header to perform a client-side redirect without a full-page reload.

public HtmxResponse Location(string path)

Parameters

path string

The path to request, or the serialized JSON options to assign to the header.

Returns

HtmxResponse

The current HtmxResponse instance.

Location(string, HtmxLocationOptions)

Sets the HX-Location header to perform a client-side redirect without a full-page reload.

public HtmxResponse Location(string path, HtmxLocationOptions options)

Parameters

path string

The path to request.

options HtmxLocationOptions

The options used to issue the request.

Returns

HtmxResponse

The current HtmxResponse instance.

Examples

[HttpPost]
public IActionResult Save(ProductInput input)
{
    var product = catalog.Update(input);

    // Load the details fragment into the page, but show the product page URL
    // in the address bar so that a reload or a shared link opens the full page.
    Response.Htmx(htmx => htmx.Location(
        Url.Action(nameof(Details), new { id = product.Id })!,
        new HtmxLocationOptions
        {
            Target = "#product-details",
            Swap = HtmxSwap.OuterHtml,
            HistoryUrl = Url.Action(nameof(Show), new { id = product.Id })
        }));

    return NoContent();
}

PreventHistoryUpdate()

Sets the HX-Push-Url header to "false" to prevent the browser's history from being updated.

public HtmxResponse PreventHistoryUpdate()

Returns

HtmxResponse

The current HtmxResponse instance.

Examples

[HttpPost("/orders")]
public IActionResult Create(OrderInput input)
{
    if (!ModelState.IsValid)
    {
        // The boosted form on /orders/new would push /orders, the POST URL,
        // into the history. Keep /orders/new so that a reload shows the form again.
        Response.Htmx(htmx => htmx.PreventHistoryUpdate());
        return View("New", input);
    }

    var order = orders.Create(input);
    return RedirectToAction(nameof(Details), new { id = order.Id });
}

Remarks

Also removes the HX-Replace-Url header and the legacy HX-Push header. HTMX 1.9.x and 2.x check HX-Push before the other history headers. When PushUrl(string), ReplaceUrl(string), and PreventHistoryUpdate() are combined, the last call takes effect.

The history is not updated even when the hx-push-url or hx-replace-url attribute or boosted navigation requests it.

PushUrl(string)

Sets the HX-Push-Url header to push a new URL onto the browser's history stack.

public HtmxResponse PushUrl(string url)

Parameters

url string

A relative or same-origin absolute URL to push into the location bar, as supported by history.pushState().

Returns

HtmxResponse

The current HtmxResponse instance.

Remarks

Also removes the HX-Replace-Url header and the legacy HX-Push header. HTMX 1.9.x and 2.x check HX-Push before the other history headers. When PushUrl(string), ReplaceUrl(string), and PreventHistoryUpdate() are combined, the last call takes effect.

HTMX treats "false" as a control value; use PreventHistoryUpdate() instead. HTMX 4.x also treats "true" as a control value and replaces it with the response URL, whereas HTMX 1.9.x and 2.x push it as the relative URL true.

Redirect(string)

Sets the HX-Redirect header to perform a client-side redirect with a full-page reload.

public HtmxResponse Redirect(string url)

Parameters

url string

The URL to redirect to.

Returns

HtmxResponse

The current HtmxResponse instance.

Examples

[HttpPost]
public IActionResult SignIn(LoginInput input)
{
    if (!auth.TrySignIn(input))
    {
        ModelState.AddModelError("", "Invalid email or password.");
        return Request.IsHtmxRequest()
            ? PartialView("_SignInForm", input)
            : View(input);
    }

    var url = Url.Action("Index", "Dashboard")!;

    if (Request.IsHtmxRequest())
    {
        // HTMX cannot read headers from a 3xx response, so send HX-Redirect with 200.
        Response.Htmx(htmx => htmx.Redirect(url));
        return Ok();
    }

    return Redirect(url);
}

Refresh()

Sets the HX-Refresh header to request a full-page refresh.

public HtmxResponse Refresh()

Returns

HtmxResponse

The current HtmxResponse instance.

ReplaceUrl(string)

Sets the HX-Replace-Url header to replace the current URL.

public HtmxResponse ReplaceUrl(string url)

Parameters

url string

A relative or same-origin absolute URL to replace the current URL in the location bar, as supported by history.replaceState().

Returns

HtmxResponse

The current HtmxResponse instance.

Examples

public IActionResult OnGetSearch(string query = "")
{
    // Keep the query in the address bar without adding a history entry for every keystroke.
    // The page must render the same results when /Catalog?query=... is opened directly.
    Response.Htmx(htmx => htmx.ReplaceUrl(Url.Page("/Catalog", new { query })!));

    return Partial("_SearchResults", catalog.Search(query));
}

Remarks

Also removes the HX-Push-Url header and the legacy HX-Push header. HTMX 1.9.x and 2.x check HX-Push before the other history headers. When PushUrl(string), ReplaceUrl(string), and PreventHistoryUpdate() are combined, the last call takes effect.

HTMX treats "false" as a control value; use PreventHistoryUpdate() instead. HTMX 4.x also treats "true" as a control value and replaces it with the response URL, whereas HTMX 1.9.x and 2.x use it as the relative URL true.

Reselect(string)

Sets the HX-Reselect header to select the part of the response to swap in.

public HtmxResponse Reselect(string selector)

Parameters

selector string

The CSS selector to assign to the header.

Returns

HtmxResponse

The current HtmxResponse instance.

Reswap(HtmxSwap)

Sets the HX-Reswap header to specify how the response will be swapped.

public HtmxResponse Reswap(HtmxSwap swap)

Parameters

swap HtmxSwap

The swap style to assign to the header.

Returns

HtmxResponse

The current HtmxResponse instance.

Examples

Response.Htmx(htmx => htmx
    .Retarget("#validation-errors")
    .Reswap(HtmxSwap.InnerHtml));

return PartialView("_ValidationSummary", ModelState);

Reswap(string)

Sets the HX-Reswap header to specify how the response will be swapped.

public HtmxResponse Reswap(string swap)

Parameters

swap string

The swap style, optionally followed by swap modifiers.

Returns

HtmxResponse

The current HtmxResponse instance.

Retarget(string)

Sets the HX-Retarget header to change the target of the content update to a different element on the page.

public HtmxResponse Retarget(string selector)

Parameters

selector string

The CSS selector to assign to the header.

Returns

HtmxResponse

The current HtmxResponse instance.

TriggerEvent(string, HtmxTriggerTiming)

Adds a client-side event to the response header selected by timing.

public HtmxResponse TriggerEvent(string eventName, HtmxTriggerTiming timing = HtmxTriggerTiming.Receive)

Parameters

eventName string

The event name to trigger.

timing HtmxTriggerTiming

The event timing. Defaults to Receive.

Returns

HtmxResponse

The current HtmxResponse instance.

Examples

public IActionResult OnPostSave()
{
    _profiles.DisplayName = Input.DisplayName;

    // Components that listen for "profile-saved" reload themselves
    // after the form is swapped. The event needs no data.
    Response.Htmx(htmx => htmx.TriggerEvent("profile-saved", HtmxTriggerTiming.AfterSwap));

    return Partial("_ProfileForm", this);
}
@addTagHelper *, Ramstack.HtmxToolkit

@* Place the listener outside the form, so the save response does not replace it. *@
<aside hx-get
       asp-page="/Profile"
       asp-page-handler="Summary"
       hx-trigger="profile-saved from:body">
    ...
</aside>

Remarks

HTMX 4.x has only the HX-Trigger header, so events with any HtmxTriggerTiming value are emitted through it. These events fire when the request completes, which is after the swap and settle steps whenever a swap is performed. See PR #3900.

Events in the same header are triggered in the order they were added. If the same event name is added more than once for a header, all events in that header are sent in a form that only the Toolkit script (htmx-toolkit.js) can replay; without the script, none of them is triggered. In HTMX 4.x, this also applies to a name added with different timings.

Each call sets the header in Headers, replacing its value with the events added for it. If the header has been removed, for example by Clear(HttpResponse), the events added before that are discarded. Like any other response header, an event cannot be added after the response has started; the method then throws InvalidOperationException.

TriggerEvent(string, object, HtmxTriggerTiming)

Adds a client-side event and its detail to the response header selected by timing.

[RequiresUnreferencedCode("Event details are serialized using reflection. Use the TriggerEvent overload that accepts JsonTypeInfo<T> for trimmed applications.")]
public HtmxResponse TriggerEvent(string eventName, object detail, HtmxTriggerTiming timing = HtmxTriggerTiming.Receive)

Parameters

eventName string

The event name to trigger.

detail object

The event detail, serialized with the Web defaults, as ASP.NET Core serializes JSON responses: property names are camel-cased, while dictionary keys and null values are written as is.

timing HtmxTriggerTiming

The event timing. Defaults to Receive.

Returns

HtmxResponse

The current HtmxResponse instance.

Examples

Response.Htmx(htmx =>
    htmx.TriggerEvent("product-saved", new { product.Id }, HtmxTriggerTiming.AfterSwap));

return PartialView("_ProductRow", product);

Remarks

HTMX 4.x has only the HX-Trigger header, so events with any HtmxTriggerTiming value are emitted through it. These events fire when the request completes, which is after the swap and settle steps whenever a swap is performed. See PR #3900.

Events in the same header are triggered in the order they were added. If the same event name is added more than once for a header, all events in that header are sent in a form that only the Toolkit script (htmx-toolkit.js) can replay; without the script, none of them is triggered. In HTMX 4.x, this also applies to a name added with different timings.

Each call sets the header in Headers, replacing its value with the events added for it. If the header has been removed, for example by Clear(HttpResponse), the events added before that are discarded. Like any other response header, an event cannot be added after the response has started; the method then throws InvalidOperationException.

TriggerEvent(string, object, JsonSerializerOptions, HtmxTriggerTiming)

Adds a client-side event and its detail, serialized with the specified JSON options, to the response header selected by timing.

[RequiresUnreferencedCode("Event details are serialized using reflection. Use the TriggerEvent overload that accepts JsonTypeInfo<T> for trimmed applications.")]
public HtmxResponse TriggerEvent(string eventName, object detail, JsonSerializerOptions options, HtmxTriggerTiming timing = HtmxTriggerTiming.Receive)

Parameters

eventName string

The event name to trigger.

detail object

The event detail.

options JsonSerializerOptions

The options used to serialize the event detail, for example, the JSON options of the application.

timing HtmxTriggerTiming

The event timing. Defaults to Receive.

Returns

HtmxResponse

The current HtmxResponse instance.

Examples

public sealed class ProductsController(IOptions<JsonOptions> jsonOptions) : Controller
{
    [HttpPost]
    public IActionResult Save(Product product)
    {
        Response.Htmx(htmx => htmx.TriggerEvent(
            "product-saved",
            new { product.Id, product.Status },
            jsonOptions.Value.JsonSerializerOptions));

        return PartialView("_ProductRow", product);
    }
}

Remarks

HTMX 4.x has only the HX-Trigger header, so events with any HtmxTriggerTiming value are emitted through it. These events fire when the request completes, which is after the swap and settle steps whenever a swap is performed. See PR #3900.

Events in the same header are triggered in the order they were added. If the same event name is added more than once for a header, all events in that header are sent in a form that only the Toolkit script (htmx-toolkit.js) can replay; without the script, none of them is triggered. In HTMX 4.x, this also applies to a name added with different timings.

Each call sets the header in Headers, replacing its value with the events added for it. If the header has been removed, for example by Clear(HttpResponse), the events added before that are discarded. Like any other response header, an event cannot be added after the response has started; the method then throws InvalidOperationException.

The detail is always written as compact JSON. Response headers allow only ASCII characters: other characters in string values and dictionary keys are escaped, but property names and enum names are written as escaped by the Encoder of options. With a relaxed encoder, such as UnsafeRelaxedJsonEscaping, these names must contain only ASCII characters.

TriggerEvent<T>(string, T, HtmxTriggerTiming)

Adds a client-side event and its detail to the response header selected by timing.

[RequiresUnreferencedCode("Event details are serialized using reflection. Use the TriggerEvent overload that accepts JsonTypeInfo<T> for trimmed applications.")]
public HtmxResponse TriggerEvent<T>(string eventName, T detail, HtmxTriggerTiming timing = HtmxTriggerTiming.Receive)

Parameters

eventName string

The event name to trigger.

detail T

The event detail, serialized with the Web defaults, as ASP.NET Core serializes JSON responses: property names are camel-cased, while dictionary keys and null values are written as is.

timing HtmxTriggerTiming

The event timing. Defaults to Receive.

Returns

HtmxResponse

The current HtmxResponse instance.

Type Parameters

T

The event detail type.

Examples

Response.Htmx(htmx =>
    htmx.TriggerEvent("product-saved", new { product.Id }, HtmxTriggerTiming.AfterSwap));

return PartialView("_ProductRow", product);

Remarks

HTMX 4.x has only the HX-Trigger header, so events with any HtmxTriggerTiming value are emitted through it. These events fire when the request completes, which is after the swap and settle steps whenever a swap is performed. See PR #3900.

Events in the same header are triggered in the order they were added. If the same event name is added more than once for a header, all events in that header are sent in a form that only the Toolkit script (htmx-toolkit.js) can replay; without the script, none of them is triggered. In HTMX 4.x, this also applies to a name added with different timings.

Each call sets the header in Headers, replacing its value with the events added for it. If the header has been removed, for example by Clear(HttpResponse), the events added before that are discarded. Like any other response header, an event cannot be added after the response has started; the method then throws InvalidOperationException.

TriggerEvent<T>(string, T, JsonTypeInfo<T>, HtmxTriggerTiming)

Adds a client-side event and its detail, serialized with the specified JSON metadata, to the response header selected by timing.

public HtmxResponse TriggerEvent<T>(string eventName, T detail, JsonTypeInfo<T> jsonTypeInfo, HtmxTriggerTiming timing = HtmxTriggerTiming.Receive)

Parameters

eventName string

The event name to trigger.

detail T

The event detail.

jsonTypeInfo JsonTypeInfo<T>

The JSON metadata for the event detail. Source-generated metadata avoids runtime reflection and is compatible with trimming and Native AOT.

timing HtmxTriggerTiming

The event timing. Defaults to Receive.

Returns

HtmxResponse

The current HtmxResponse instance.

Type Parameters

T

The event detail type.

Examples

var detail = new ProductSaved(product.Id);

Response.Htmx(
    static (htmx, detail) => htmx.TriggerEvent(
        "product-saved",
        detail,
        AppJsonContext.Default.ProductSaved,
        HtmxTriggerTiming.AfterSwap),
    detail);

[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
[JsonSerializable(typeof(ProductSaved))]
internal partial class AppJsonContext : JsonSerializerContext;

internal sealed record ProductSaved(int Id);

Remarks

HTMX 4.x has only the HX-Trigger header, so events with any HtmxTriggerTiming value are emitted through it. These events fire when the request completes, which is after the swap and settle steps whenever a swap is performed. See PR #3900.

Events in the same header are triggered in the order they were added. If the same event name is added more than once for a header, all events in that header are sent in a form that only the Toolkit script (htmx-toolkit.js) can replay; without the script, none of them is triggered. In HTMX 4.x, this also applies to a name added with different timings.

Each call sets the header in Headers, replacing its value with the events added for it. If the header has been removed, for example by Clear(HttpResponse), the events added before that are discarded. Like any other response header, an event cannot be added after the response has started; the method then throws InvalidOperationException.

Response headers allow only ASCII characters. Other characters in string values and dictionary keys are escaped, but property names and enum names are written as escaped by the Encoder of the options that jsonTypeInfo belongs to. With a relaxed encoder, such as UnsafeRelaxedJsonEscaping, these names must contain only ASCII characters.