首页 > 解决方案 > 将 TagBuilder 核心方法从 ASP.NET MVC 5 迁移到 ASP.NET Core (.NET 5)

问题描述

我有一个使用 ASP.NET MVC 5 开发的 Web 应用程序,我必须将其迁移到 ASP.NET Core (.NET 5)。我有一些 HtmlHelper 的扩展方法来获取带有链接的字形图标:

public static MvcHtmlString GlyphiconActionLink(this HtmlHelper helper, string action, string controller, object parameters, string glyphiconTag, string title)
{
    TagBuilder span = new TagBuilder("span");
    span.MergeAttribute("class", $"glyphicon {glyphiconTag}");
    span.MergeAttribute("title", title);

    string url = UrlHelper.GenerateUrl(
        null, action, controller, new RouteValueDictionary(parameters),
        helper.RouteCollection, helper.ViewContext.RequestContext, true);

    TagBuilder link = new TagBuilder("a");
    link.MergeAttribute("href", url);
    link.InnerHtml = span.ToString(TagRenderMode.SelfClosing);

    return MvcHtmlString.Create(link.ToString());
}

在 Razor pages 我可以写这个

@Html.GlyphiconActionLink("Details", "MyController", new { id = model.Id }, "glyphicon-pencil", "Show to details")

要获取此 HTML:

<a href="/mycontroller/details/1"><span class="glyphicon glyphicon-pencil" title="Details" /></a>

现在,在 .NET Core 中,此代码不起作用,它有很多错误。

我所做的改变:

我遇到的错误是:

你知道如何迁移它吗?

标签: c#asp.net-core-mvcglyphiconsactionlink

解决方案


推荐阅读