Enclose a list of tags (taghelper) by a parent tag

2019-08-14 03:23发布

问题:

I need to achieve like below

<MyTagList>
    <MyTag></MyTag>
    <MyTag></MyTag>
</MyTagList>

When ever I use MyTag in design time, it need to be enclosed automaticaly by MyTagList or MyTag should error that it must be enclosed by MyTagList

回答1:

You can't do this today at design time. You can however do it partially at runtime by utilizing TagHelperContext.Items. By using the Items property you can ensure that each tag is enclosed by a parent tag and throw if it's not. Insert an indicator into the Items bag in your parent element and verify that it exists in child elements.

As for supporting this in the future there are two issues open to add design time support:

  • https://github.com/aspnet/Razor/issues/255
  • https://github.com/aspnet/Razor/issues/474

Hope this helps!