Table of Contents

Class HtmxRequestAttribute

Namespace
Ramstack.HtmxToolkit
Assembly
Ramstack.HtmxToolkit.dll

Restricts an action to HTMX requests.

[AttributeUsage(AttributeTargets.Method)]
public sealed class HtmxRequestAttribute : Attribute, IActionConstraint, IActionConstraintMetadata
Inheritance
HtmxRequestAttribute
Implements
Inherited Members

Examples

// A widget polls this URL; it is not a page. The action is selected only
// for HTMX requests, so a direct browser visit gets 404 instead of a bare fragment.
[HttpGet("/orders/{id:int}/status")]
[HtmxRequest]
public IActionResult Status(int id) =>
    PartialView("_OrderStatus", orders.GetStatus(id));

Properties

BoostedFilter

Gets or sets the boosted navigation filter for the action.

public HtmxBoostedFilter BoostedFilter { get; set; }

Property Value

HtmxBoostedFilter

Examples

[HtmxRequest(BoostedFilter = HtmxBoostedFilter.Boosted)]
public IActionResult Navigation() =>
    PartialView("_Navigation", menu.Items);

Remarks

  • Any, the default, does not restrict boosted navigation.
  • Boosted accepts only boosted HTMX requests.
  • NonBoosted accepts only non-boosted HTMX requests.

Order

The constraint order.

public int Order { get; }

Property Value

int

Remarks

Constraints are grouped into stages by the value of Order. See remarks on IActionConstraint.

RequestType

Gets or sets the type of HTMX request accepted by the action.

public HtmxRequestType RequestType { get; set; }

Property Value

HtmxRequestType

Examples

[HttpGet("/profile")]
public IActionResult Profile() =>
    View();

[HttpGet("/profile")]
[HtmxRequest(RequestType = HtmxRequestType.Partial)]
public IActionResult ProfileFragment() =>
    PartialView("_Profile");

Remarks

The default, Unspecified, does not restrict the request type. Other values require a matching RequestType.

HTMX 1.x and 2.x send the HX-Request-Type header only when the page includes the companion script htmx-toolkit.js. When BoostedFilter is also set, both conditions must be satisfied.

Methods

Accept(ActionConstraintContext)

Determines whether an action is a valid candidate for selection.

public bool Accept(ActionConstraintContext context)

Parameters

context ActionConstraintContext

The ActionConstraintContext.

Returns

bool

True if the action is valid for selection, otherwise false.