Table of Contents

Class HtmxUrlTagHelper

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

Generates URLs for HTMX request attributes from the standard ASP.NET Core asp-* routing attributes.

[HtmlTargetElement(Attributes = "hx-get,asp-action")]
[HtmlTargetElement(Attributes = "hx-get,asp-controller")]
[HtmlTargetElement(Attributes = "hx-get,asp-area")]
[HtmlTargetElement(Attributes = "hx-get,asp-page")]
[HtmlTargetElement(Attributes = "hx-get,asp-page-handler")]
[HtmlTargetElement(Attributes = "hx-get,asp-route")]
[HtmlTargetElement(Attributes = "hx-get,asp-all-route-data")]
[HtmlTargetElement(Attributes = "hx-get,asp-route-*")]
[HtmlTargetElement(Attributes = "hx-get,asp-fragment")]
[HtmlTargetElement(Attributes = "hx-post,asp-action")]
[HtmlTargetElement(Attributes = "hx-post,asp-controller")]
[HtmlTargetElement(Attributes = "hx-post,asp-area")]
[HtmlTargetElement(Attributes = "hx-post,asp-page")]
[HtmlTargetElement(Attributes = "hx-post,asp-page-handler")]
[HtmlTargetElement(Attributes = "hx-post,asp-route")]
[HtmlTargetElement(Attributes = "hx-post,asp-all-route-data")]
[HtmlTargetElement(Attributes = "hx-post,asp-route-*")]
[HtmlTargetElement(Attributes = "hx-post,asp-fragment")]
[HtmlTargetElement(Attributes = "hx-put,asp-action")]
[HtmlTargetElement(Attributes = "hx-put,asp-controller")]
[HtmlTargetElement(Attributes = "hx-put,asp-area")]
[HtmlTargetElement(Attributes = "hx-put,asp-page")]
[HtmlTargetElement(Attributes = "hx-put,asp-page-handler")]
[HtmlTargetElement(Attributes = "hx-put,asp-route")]
[HtmlTargetElement(Attributes = "hx-put,asp-all-route-data")]
[HtmlTargetElement(Attributes = "hx-put,asp-route-*")]
[HtmlTargetElement(Attributes = "hx-put,asp-fragment")]
[HtmlTargetElement(Attributes = "hx-patch,asp-action")]
[HtmlTargetElement(Attributes = "hx-patch,asp-controller")]
[HtmlTargetElement(Attributes = "hx-patch,asp-area")]
[HtmlTargetElement(Attributes = "hx-patch,asp-page")]
[HtmlTargetElement(Attributes = "hx-patch,asp-page-handler")]
[HtmlTargetElement(Attributes = "hx-patch,asp-route")]
[HtmlTargetElement(Attributes = "hx-patch,asp-all-route-data")]
[HtmlTargetElement(Attributes = "hx-patch,asp-route-*")]
[HtmlTargetElement(Attributes = "hx-patch,asp-fragment")]
[HtmlTargetElement(Attributes = "hx-delete,asp-action")]
[HtmlTargetElement(Attributes = "hx-delete,asp-controller")]
[HtmlTargetElement(Attributes = "hx-delete,asp-area")]
[HtmlTargetElement(Attributes = "hx-delete,asp-page")]
[HtmlTargetElement(Attributes = "hx-delete,asp-page-handler")]
[HtmlTargetElement(Attributes = "hx-delete,asp-route")]
[HtmlTargetElement(Attributes = "hx-delete,asp-all-route-data")]
[HtmlTargetElement(Attributes = "hx-delete,asp-route-*")]
[HtmlTargetElement(Attributes = "hx-delete,asp-fragment")]
[HtmlTargetElement(Attributes = "hx-action,asp-action")]
[HtmlTargetElement(Attributes = "hx-action,asp-controller")]
[HtmlTargetElement(Attributes = "hx-action,asp-area")]
[HtmlTargetElement(Attributes = "hx-action,asp-page")]
[HtmlTargetElement(Attributes = "hx-action,asp-page-handler")]
[HtmlTargetElement(Attributes = "hx-action,asp-route")]
[HtmlTargetElement(Attributes = "hx-action,asp-all-route-data")]
[HtmlTargetElement(Attributes = "hx-action,asp-route-*")]
[HtmlTargetElement(Attributes = "hx-action,asp-fragment")]
[RequiresUnreferencedCode("Razor Page URL generation is not compatible with trimming.")]
public sealed class HtmxUrlTagHelper : TagHelper, ITagHelper, ITagHelperComponent
Inheritance
HtmxUrlTagHelper
Implements
Inherited Members

Examples

@addTagHelper *, Ramstack.HtmxToolkit

<button hx-post
        asp-page="/Orders/Edit"
        asp-page-handler="Archive"
        asp-route-id="@Model.OrderId"
        hx-target="#order-status">
    Archive
</button>

@* For order 42, renders hx-post="/Orders/Edit?id=42&amp;handler=Archive". *@

Remarks

The tag helper applies to elements that have an hx-get, hx-post, hx-put, hx-patch, hx-delete, or HTMX 4.x hx-action attribute together with at least one of the asp-action, asp-controller, asp-area, asp-page, asp-page-handler, asp-route, asp-route-*, asp-all-route-data, or asp-fragment attributes. The generated URL is written to the HTMX attribute, which must not have a value.

On <a>, <form>, <button>, and submit or image <input> elements, the built-in ASP.NET Core tag helpers process the same asp-* attributes and also render href, action, or formaction.

An InvalidOperationException is thrown when more than one HTMX request attribute is present, when the HTMX request attribute has a value, even an empty one, when hx-action is used with a version other than HTMX 4.x, or when attributes from more than one of the following groups are specified: asp-route; asp-controller and asp-action; asp-page and asp-page-handler.

Constructors

HtmxUrlTagHelper(IUrlHelperFactory, IOptions<HtmxToolkitOptions>)

Generates URLs for HTMX request attributes from the standard ASP.NET Core asp-* routing attributes.

public HtmxUrlTagHelper(IUrlHelperFactory factory, IOptions<HtmxToolkitOptions> options)

Parameters

factory IUrlHelperFactory

The factory used to create URL helpers.

options IOptions<HtmxToolkitOptions>

The configured HtmxToolkit options.

Remarks

The tag helper applies to elements that have an hx-get, hx-post, hx-put, hx-patch, hx-delete, or HTMX 4.x hx-action attribute together with at least one of the asp-action, asp-controller, asp-area, asp-page, asp-page-handler, asp-route, asp-route-*, asp-all-route-data, or asp-fragment attributes. The generated URL is written to the HTMX attribute, which must not have a value.

On <a>, <form>, <button>, and submit or image <input> elements, the built-in ASP.NET Core tag helpers process the same asp-* attributes and also render href, action, or formaction.

An InvalidOperationException is thrown when more than one HTMX request attribute is present, when the HTMX request attribute has a value, even an empty one, when hx-action is used with a version other than HTMX 4.x, or when attributes from more than one of the following groups are specified: asp-route; asp-controller and asp-action; asp-page and asp-page-handler.

Properties

Action

Gets or sets the name of the action.

[HtmlAttributeName("asp-action")]
public string? Action { get; set; }

Property Value

string

Remarks

Must be null if Route, Page, or PageHandler is not null.

Area

Gets or sets the name of the area.

[HtmlAttributeName("asp-area")]
public string? Area { get; set; }

Property Value

string

Remarks

Can be combined with a named route, controller action, or Razor Page. When not null, overrides any area value in RouteValues.

Controller

Gets or sets the name of the controller.

[HtmlAttributeName("asp-controller")]
public string? Controller { get; set; }

Property Value

string

Remarks

Must be null if Route, Page, or PageHandler is not null.

Fragment

Gets or sets the URL fragment name.

[HtmlAttributeName("asp-fragment")]
public string? Fragment { get; set; }

Property Value

string

Order

When a set of ITagHelpers are executed, their Init(TagHelperContext)'s are first invoked in the specified Order; then their ProcessAsync(TagHelperContext, TagHelperOutput)'s are invoked in the specified Order. Lower values are executed first.

public override int Order { get; }

Property Value

int

Remarks

Default order is 0.

Page

Gets or sets the name of the page.

[HtmlAttributeName("asp-page")]
public string? Page { get; set; }

Property Value

string

Remarks

Must be null if Route, Action, or Controller is not null.

PageHandler

Gets or sets the name of the page handler.

[HtmlAttributeName("asp-page-handler")]
public string? PageHandler { get; set; }

Property Value

string

Remarks

Must be null if Route, Action, or Controller is not null.

Route

Gets or sets the name of the route.

[HtmlAttributeName("asp-route")]
public string? Route { get; set; }

Property Value

string

Remarks

Must be null if Action, Controller, Page, or PageHandler is not null.

RouteValues

Gets or sets the additional route values.

[HtmlAttributeName("asp-all-route-data", DictionaryAttributePrefix = "asp-route-")]
public IDictionary<string, string> RouteValues { get; set; }

Property Value

IDictionary<string, string>

ViewContext

Gets or sets the view context for the current request.

[ViewContext]
[HtmlAttributeNotBound]
public ViewContext ViewContext { get; set; }

Property Value

ViewContext

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