Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/articles/responses.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ public IActionResult Inventory(string sku) =>
The attribute supports `Refresh`, `Reswap`, `ReswapExpression`, `Retarget`, and `Reselect`.
Use the fluent API when values depend on runtime state or when triggering events and navigation.

Apply the attribute to an action, a controller, or a Razor Pages page model.
Razor Pages ignore filter attributes on handler methods, so put it on the page model class.
When a controller and its action specify the same header, the action's value takes effect.
A derived class inherits the attribute from its base class; an attribute declared on the derived class
replaces the inherited one entirely.

## Access headers directly

Use `Response.GetHtmxHeaders()` when middleware or shared infrastructure needs property access instead of fluent chaining:
Expand Down
20 changes: 16 additions & 4 deletions src/Ramstack.HtmxToolkit/HtmxResponseAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,23 @@ namespace Ramstack.HtmxToolkit;
/// Specifies HTMX response headers to apply when an action result is executed for an HTMX request.
/// </summary>
/// <remarks>
/// A property that is not set, or is set to <see langword="false" />, <see langword="null" />,
/// or an empty string, adds no header. The specified headers replace any values that the action
/// has already set for them.
/// <para>
/// A property that is not set, or is set to <see langword="false" />, <see langword="null" />,
/// or an empty string, adds no header. The specified headers replace any values that the action
/// has already set for them.
/// </para>
/// <para>
/// 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.
/// </para>
/// <para>
/// 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.
/// </para>
/// </remarks>
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public sealed class HtmxResponseAttribute : Attribute, IResultFilter
{
private readonly List<(string Key, string Value)> _headers = [];
Expand Down
Loading