Table of Contents

Class HtmxResponseAttribute

Namespace
Ramstack.HtmxToolkit
Assembly
Ramstack.HtmxToolkit.dll

Specifies HTMX response headers to apply to the response to an HTMX request before the action or page handler runs.

[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public sealed class HtmxResponseAttribute : Attribute, IActionFilter, IPageFilter, IFilterMetadata
Inheritance
HtmxResponseAttribute
Implements
Inherited Members

Examples

[HtmxRequest]
[HtmxResponse(
    Retarget = "#inventory-status",
    Reswap = HtmxSwap.OuterHtml)]
[HttpGet("/inventory/{sku}")]
public IActionResult Inventory(string sku) =>
    PartialView("_Inventory", inventory.Find(sku));

Remarks

A property that is not set, or is set to false, null, or an empty string, adds no header. Because the headers are set before the action or page handler runs, the code can replace or remove them, for example with Htmx(HttpResponse, Action<HtmxResponse>). Headers are not set if another filter short-circuits execution before this filter runs.

The attribute can be applied to an action, a controller, or a Razor Pages page model. Razor Pages ignore filter attributes on handler methods, so apply it to the page model class. When both a controller and its action specify the same header, the action's value takes effect.

A derived controller or page model inherits the attribute from its base class. If the derived class declares its own attribute, that attribute replaces the inherited one, including the headers that only the inherited attribute specifies.

Properties

Refresh

Gets or sets a value indicating whether the HX-Refresh header is set to request a full-page refresh.

public bool Refresh { get; set; }

Property Value

bool

Reselect

Gets or sets the CSS selector to specify in the HX-Reselect header.

[MaybeNull]
public string Reselect { get; set; }

Property Value

string

Reswap

Gets or sets the swap style to specify in the HX-Reswap header.

public HtmxSwap Reswap { get; set; }

Property Value

HtmxSwap

Remarks

Returns InnerHtml if no swap style is specified or the style is not recognized. ReswapExpression sets the same header, so specify only one of them.

ReswapExpression

Gets or sets the complete HX-Reswap header value, including any swap modifiers.

[MaybeNull]
public string ReswapExpression { get; set; }

Property Value

string

Remarks

Reswap sets the same header, so specify only one of them.

Retarget

Gets or sets the CSS selector to specify in the HX-Retarget header.

[MaybeNull]
public string Retarget { get; set; }

Property Value

string

Methods

OnActionExecuted(ActionExecutedContext)

Called after the action executes, before the action result.

public void OnActionExecuted(ActionExecutedContext context)

Parameters

context ActionExecutedContext

The ActionExecutedContext.

OnActionExecuting(ActionExecutingContext)

Called before the action executes, after model binding is complete.

public void OnActionExecuting(ActionExecutingContext context)

Parameters

context ActionExecutingContext

The ActionExecutingContext.

OnPageHandlerExecuted(PageHandlerExecutedContext)

Called after the handler method executes, before the action result executes.

public void OnPageHandlerExecuted(PageHandlerExecutedContext context)

Parameters

context PageHandlerExecutedContext

The PageHandlerExecutedContext.

OnPageHandlerExecuting(PageHandlerExecutingContext)

Called before the handler method executes, after model binding is complete.

public void OnPageHandlerExecuting(PageHandlerExecutingContext context)

Parameters

context PageHandlerExecutingContext

The PageHandlerExecutingContext.

OnPageHandlerSelected(PageHandlerSelectedContext)

Called after a handler method has been selected, but before model binding occurs.

public void OnPageHandlerSelected(PageHandlerSelectedContext context)

Parameters

context PageHandlerSelectedContext

The PageHandlerSelectedContext.