我可以在 returns html 的自定义 Tag Helper 中使用 Tag Helper 吗?
Can I use a Tag Helper in a custom Tag Helper that returns html?
我最近 运行 遇到了一种情况,我想在标签助手中使用标签助手。我环顾四周,找不到其他人尝试这样做,我是在使用糟糕的约定还是缺少文档?
例如。 标签助手 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
的所有内置优点。
<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);
}
}
我最近 运行 遇到了一种情况,我想在标签助手中使用标签助手。我环顾四周,找不到其他人尝试这样做,我是在使用糟糕的约定还是缺少文档?
例如。 标签助手 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
的所有内置优点。
<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);
}
}