I am writing a set of ASP.Net Core tag helpers that target (among other tags) <form> and <input> tags. My <form> tag helper defines a custom attribute, the value of which it wants to pass to the child elements.
All of the articles I've read make this sound simple: The parent tag helper stores the value in the context.Items dictionary, and the children read it from that same dictionary.
This implies that the child tag helpers execute after the parent tag helper. However, I've discovered that, in the case of <form> and <input> tag helpers, the FormTagHelper executes after the InputTagHelper.
By way of example, consider this HTML:
<form my-attr='Hello'>
<input asp-for='SomeProperty' />
</form>
My form tag helper:
public class FormTagHelper : TagHelper
{
public string MyAttr { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
Debug.WriteLine("<form>");
context.Items.Add("my-attr", MyAttr ?? "");
}
}
Input tag helper:
public class InputTagHelper : TagHelper
{
public override void Process(TagHelperContext context, TagHelperOutput output)
{
Debug.WriteLine("<input>");
var valueFromParentForm = context.Items["my-attr"].ToString();
}
}
I would expect valueFromParentForm to be "Hello", but in fact it throws an exception because the context.Items dictionary is empty.
What is this is all about, and what might I do to work around this weird, inside-out order of execution?


