c# - 在 TagHelper 中获取标签内容

标签 c# asp.net-core asp.net-core-tag-helpers

如果我有这样的标签:

<foo>Some content</foo>

如何获取 TagHelper 中的内容?

我在 TagHelperTagHelperContext 上看不到任何内容。

我正在尝试解析标签的内容。

最佳答案

解决方案有点不直观,通过 TagHelperOutput.GetChildContentAsync() 方法从 TagHelperOutput 获取内容。

如果我们有这样的标签:

<my-tag>Some content</my-tag>

然后

public override void Process(TagHelperContext context, TagHelperOutput output)
{
    var childContext = output.GetChildContentAsync().Result;
    var content = childContext.GetContent();
    // content == "Some content"
}

关于c# - 在 TagHelper 中获取标签内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48770880/

相关文章:

asp.net-mvc - 带有查询字符串参数的 asp.net core mvc 应用程序表单帖子

c# - TcpClient.Close() 仅适用于 Thread.Sleep()

c# - NpGsql Entity Framework 6 - "An operation is already in progress"

asp.net - 将 dotnet 核心部署到 Heroku

c# - 如何使用标签助手创建带有模型绑定(bind)的自定义编辑器

c# - 为 asp-for 指定自定义名称(仅需要子属性)

c# - 如何将参数传递给 RazorPage OnGet()?

c# - Entity Framework Core 自定义脚手架

c# - 以系统用户身份启动进程

c# - ASP.NET Core API Swagger - 未找到网址 : https://localhost:44358 的网页