Table of Contents

Class HtmxRequestTagHelper

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

Renders request options as hx-request (HTMX 1.9.x and 2.x) or hx-config (HTMX 4.x).

[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.9.x and 2.x: hx-request='{"timeout":2000}'
   HTMX 4.x:           hx-config='{"timeout":2000}' *@

Remarks

HTMX 1.9.x and 2.x merge-inherit hx-request from parent elements.

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

Options that the selected HTMX version does not support are not rendered. If no supported option is set, the tag helper renders no attribute.

An InvalidOperationException is thrown if the element already has the attribute for the selected HTMX version or its alias. The default alias uses the data-hx- prefix; Prefix determines the prefix in HTMX 4.x. The check also covers attributes with HTMX 4.x modifiers, such as hx-config:inherited.

Constructors

HtmxRequestTagHelper(IOptions<HtmxToolkitOptions>)

Renders request options as hx-request (HTMX 1.9.x and 2.x) or hx-config (HTMX 4.x).

public HtmxRequestTagHelper(IOptions<HtmxToolkitOptions> options)

Parameters

options IOptions<HtmxToolkitOptions>

Remarks

HTMX 1.9.x and 2.x merge-inherit hx-request from parent elements.

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

Options that the selected HTMX version does not support are not rendered. If no supported option is set, the tag helper renders no attribute.

An InvalidOperationException is thrown if the element already has the attribute for the selected HTMX version or its alias. The default alias uses the data-hx- prefix; Prefix determines the prefix in HTMX 4.x. The check also covers attributes with HTMX 4.x modifiers, such as hx-config:inherited.

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

The property does not change the generated attribute name for HTMX 1.9.x and 2.x.

For HTMX 4.x, the tag helper emits the append modifier when this property is true.

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.

If the property has not been set, the option is not rendered, HTMX uses its default value, and the property returns Default.

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.9.x and 2.x, this property maps to the boolean credentials option of hx-request. Include yields true. SameOrigin yields false. Omit is unsupported, so the option is omitted and HTMX uses its default value.

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

If the property has not been set, the option is not rendered, HTMX uses its default value, and the property returns SameOrigin.

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.9.x and 2.x merge-inherit request configuration without a modifier, so the property does not change the generated attribute name for them.

For HTMX 4.x, the tag helper 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 only in HTMX 1.9.x and 2.x.

Headers added by the browser are unaffected.

The Toolkit script also omits the HX-Request-Type header.

If the property has not been set, the option is not rendered, HTMX uses its default value, and the property returns false.

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.

If the property has not been set, the option is not rendered, HTMX uses its default value, and the property returns Follow.

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.9.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.

If the property has not been set, the option is not rendered, HTMX uses its default value, and the property returns false.

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).