首页 > 解决方案 > 如何在 Asp.NET Core 中将动态创建的 HTML 字符串呈现到剃刀视图中

问题描述

我已经编写了如下的 html 辅助方法

 public static class CustomHtmlHelper
{
    public static HtmlString SetMenuRespectiveWithRoles(this IHtmlHelper html, HttpContext context)
    {
        StringBuilder menu = new StringBuilder();
        string menuLink = string.Format("<a asp-area='{0}' asp-controller='{1}' asp-action='{2}'>{3}</a></li>", "AreaName", "ControllerName", "ActionName", "Menu Name");
        return new HtmlString(menu.ToString());
    }
}

并从布局中调用它 -

 @Html.SetMenuRespectiveWithRoles(Context)

它在浏览器上呈现为 -

<a asp-area="AreaName" asp-controller="ControllerName" asp-action="ActionName">MenuName</a>

我期待它会呈现为

<a href="AreaName/ControllerName/ActionName">MenuName</a>

我不想使用TagHelpers类创建 html 标签。

您能否指导我们如何在.net core razor 页面中呈现动态创建的字符串?

标签: asp.net-mvcasp.net-coreasp.net-core-mvc

解决方案


推荐阅读