我可以在返回html的自定义Tag Helper中使用Tag Helper吗?

Jac*_*ney 20 c# asp.net asp.net-mvc asp.net-core-mvc tag-helpers

我最近遇到了一种情况,我想在标签助手中使用标签助手.我环顾四周,找不到其他人试图这样做,我使用的是一个糟糕的惯例,还是我缺少文档?

防爆.Tag Helper A输出包含另一个标记助手的HTML.

防爆.

[HtmlTargetElement("tag-name")]
public class RazorTagHelper : TagHelper
{
    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<a asp-action=\"Home\" ");
        output.Content.SetHtmlContent(sb.ToString());
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法让我<a asp-action> </a>从C#处理标签助手?或者使用标记帮助程序重新处理输出HTML?

N. *_*len 14

你不能.TagHelpers是一个Razor解析时间功能.

另一种方法是创建TagHelper并手动调用其ProcessAsync/Process方法.又名:

var anchorTagHelper = new AnchorTagHelper
{
    Action = "Home",
};
var anchorOutput = new TagHelperOutput("a", new TagHelperAttributeList(), (useCachedResult, encoder) => new HtmlString());
var anchorContext = new TagHelperContext(
    new TagHelperAttributeList(new[] { new TagHelperAttribute("asp-action", new HtmlString("Home")) }),
    new Dictionary<object, object>(),
    Guid.NewGuid());
await anchorTagHelper.ProcessAsync(anchorContext, anchorOutput);
output.Content.SetHtmlContent(anchorOutput);
Run Code Online (Sandbox Code Playgroud)

  • 多么糟糕,如果这是唯一的方法,这是不幸的.我打算将这个问题暂时搁置一天左右,以确认这是我必须这样做的方式.如果没有其他答案,我当然会接受这个.谢谢 (2认同)

Gor*_*ski 7

如果有人希望重用 ASP.NET Core 中的内置标记帮助程序,则可以使用 IHtmlGenerator。对于重用其他类型的标签助手,我还没有找到比@N更简单的选项。泰勒·马伦的回答

以下是如何重用 asp-action 标记助手:

[HtmlTargetElement("helplink")]
public class RazorTagHelper : TagHelper
{
    private readonly IHtmlGenerator _htmlGenerator;

    public RazorTagHelper(IHtmlGenerator htmlGenerator)
    {
        _htmlGenerator = htmlGenerator;
    }

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

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        output.TagName = "div";
        output.TagMode = TagMode.StartTagAndEndTag;
        var actionAnchor = _htmlGenerator.GenerateActionLink(
            ViewContext,
            linkText: "Home",
            actionName: "Index",
            controllerName: null,
            fragment: null,
            hostname: null,
            htmlAttributes: null,
            protocol: null,
            routeValues: null
            );
        var builder = new HtmlContentBuilder();
        builder.AppendHtml("Here's the link: ");
        builder.AppendHtml(actionAnchor);
        output.Content.SetHtmlContent(builder);
    }
}
Run Code Online (Sandbox Code Playgroud)


jag*_*jag 5

我不知道这是否适用于您的方案,但是可以从AnchorTagHelper继承,然后像这样进行自定义。

public class TestTagHelper : AnchorTagHelper
{
    public TestTagHelper(IHtmlGenerator htmlGenerator) : base(htmlGenerator) { }

    public async override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
    {
        // Replaces <test> with <a> tag
        output.TagName = "a"; 
        // do custom processing
        output.Attributes.SetAttribute("class", "custom-class");
        // let the base class generate the href 
        // note the base method may override your changes so it may be  
        // preferable to call it first instead of last.
        await base.ProcessAsync(context, output);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以在视图中使用该标签助手,并具有default的所有内置优点AnchorTagHelper

<test asp-action="Index" asp-route-id="5"></test>
Run Code Online (Sandbox Code Playgroud)