繁体   English   中英

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

[英]Can I use a Tag Helper in a custom Tag Helper that returns html?

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

防爆。 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());
    }
}

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

你不能。 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);

我不知道这是否适用于您的场景,但可以从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);
    }
}

然后,您可以在视图中使用此标记助手,并具有默认AnchorTagHelper所有内置AnchorTagHelper

<test asp-action="Index" asp-route-id="5"></test>

如果有人想要重用asp.net核心的内置标记助手,你可以使用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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM