Table of Contents

Class HtmxRequestTagHelper

Namespace
Ramstack.HtmxToolkit.TagHelpers
Assembly
Ramstack.HtmxToolkit.dll

Applies version-specific HTMX request configuration to matching elements.

[HtmlTargetElement(Attributes = "hx-request-inherited")]
[HtmlTargetElement(Attributes = "hx-request-append")]
[HtmlTargetElement(Attributes = "hx-request-timeout")]
[HtmlTargetElement(Attributes = "hx-request-credentials")]
[HtmlTargetElement(Attributes = "hx-request-no-headers")]
[HtmlTargetElement(Attributes = "hx-request-cache")]
[HtmlTargetElement(Attributes = "hx-request-redirect")]
[HtmlTargetElement(Attributes = "hx-request-referrer")]
[HtmlTargetElement(Attributes = "hx-request-integrity")]
[HtmlTargetElement(Attributes = "hx-request-validate")]
public sealed class HtmxRequestTagHelper : TagHelper, ITagHelper, ITagHelperComponent
Inheritance
HtmxRequestTagHelper
Implements
Inherited Members

Examples

@addTagHelper *, Ramstack.HtmxToolkit

<button hx-get="/reports/preview"
        hx-request-timeout="2000"
        hx-target="#preview">
    Generate preview
</button>

@* HTMX 1.x and 2.x: hx-request='{"timeout":2000}'
   HTMX 4.x:        hx-config='{"timeout":2000}' *@

Remarks

HTMX 1.x and 2.x use the merge-inherited hx-request attribute.

HTMX 4.x uses hx-config, supports the inherited and append modifiers, and can enable inheritance globally with ImplicitInheritance.

Constructors

HtmxRequestTagHelper(IOptions<HtmxToolkitOptions>)

Applies version-specific HTMX request configuration to matching elements.

public HtmxRequestTagHelper(IOptions<HtmxToolkitOptions> options)

Parameters

options IOptions<HtmxToolkitOptions>

Remarks

HTMX 1.x and 2.x use the merge-inherited hx-request attribute.

HTMX 4.x uses hx-config, supports the inherited and append modifiers, and can enable inheritance globally with ImplicitInheritance.

Properties

Append

Gets or sets a value indicating whether the generated attribute is appended to inherited configuration.

[HtmlAttributeName("hx-request-append")]
public bool Append { get; set; }

Property Value

bool

Remarks

HTMX 4.x emits the append modifier when this property is true. The property does not change the generated attribute name for HTMX 1.x and 2.x.

Cache

Gets or sets the Fetch cache mode for the request.

[HtmlAttributeName("hx-request-cache")]
public HtmxRequestCache? Cache { get; set; }

Property Value

HtmxRequestCache?

Remarks

Supported only in HTMX 4.x.

Credentials

Gets or sets the credentials mode for the request.

[HtmlAttributeName("hx-request-credentials")]
public HtmxRequestCredentials? Credentials { get; set; }

Property Value

HtmxRequestCredentials?

Remarks

In HTMX 1.x and 2.x, this property maps to the boolean credentials option of hx-request. Include yields true. SameOrigin yields false.

In HTMX 4.x, this property maps to the string credentials option of hx-config.

Omit is unsupported in HTMX 1.x and 2.x, so the option is omitted and HTMX uses its default value.

Inherited

Gets or sets a value indicating whether the generated attribute is explicitly inherited.

[HtmlAttributeName("hx-request-inherited")]
public bool Inherited { get; set; }

Property Value

bool

Remarks

HTMX 1.x and 2.x merge-inherit request configuration without a modifier.

HTMX 4.x emits the inherited modifier when this property is true.

Integrity

Gets or sets the subresource integrity value for the request.

[HtmlAttributeName("hx-request-integrity")]
public string? Integrity { get; set; }

Property Value

string

Remarks

Supported only in HTMX 4.x.

NoHeaders

Gets or sets a value indicating whether headers set by HTMX are omitted, including custom headers supplied through hx-headers.

[HtmlAttributeName("hx-request-no-headers")]
public bool? NoHeaders { get; set; }

Property Value

bool?

Remarks

Supported in HTMX 1.x and 2.x. Removed in HTMX 4.x.

Headers added by the browser are unaffected.

Redirect

Gets or sets the Fetch redirect mode for the request.

[HtmlAttributeName("hx-request-redirect")]
public HtmxRequestRedirect? Redirect { get; set; }

Property Value

HtmxRequestRedirect?

Remarks

Supported only in HTMX 4.x.

Referrer

Gets or sets the Fetch API referrer value for the request.

[HtmlAttributeName("hx-request-referrer")]
public string? Referrer { get; set; }

Property Value

string

Remarks

Supported only in HTMX 4.x.

Accepts a same-origin URL, an empty string to omit the referrer, or about:client to use the default referrer. The browser replaces a URL from another origin with the default referrer.

The separate Fetch API referrerPolicy option is not configured by this property. Policy values such as no-referrer are treated as relative URLs; use an empty string to omit the referrer.

Timeout

Gets or sets the timeout for the request in milliseconds.

[HtmlAttributeName("hx-request-timeout")]
public int? Timeout { get; set; }

Property Value

int?

Remarks

Supported in HTMX 1.x, 2.x, and 4.x.

Validate

Gets or sets a value indicating whether the form is validated before submission.

[HtmlAttributeName("hx-request-validate")]
public bool? Validate { get; set; }

Property Value

bool?

Remarks

Supported only in HTMX 4.x.

Methods

ProcessAsync(TagHelperContext, TagHelperOutput)

Asynchronously executes the TagHelper with the given context and output.

public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)

Parameters

context TagHelperContext

Contains information associated with the current HTML tag.

output TagHelperOutput

A stateful HTML element used to generate an HTML tag.

Returns

Task

A Task that on completion updates the output.

Remarks

By default this calls into Process(TagHelperContext, TagHelperOutput).