【问题标题】:ASP.NET Core : HtmlHelper extension (migration issue)ASP.NET Core:HtmlHelper 扩展(迁移问题)
【发布时间】:2019-06-19 02:53:36
【问题描述】:

我正在尝试更新旧 MVC 项目 (.NET Framework 4.5.2) 中的一些代码以使用 .NET Core 2.2。我被困在 HtmlHelper 的扩展方法上,该方法在字符串中生成链接。

public static HtmlString GetMenu(this HtmlHelper htmlHelper)
{
   htmlString += string.Format("<li{0}>{1}</li>",
            controller == "Examples" ? " class=\"selected\"" : "",
            htmlHelper.ActionLink("Examples", "Index", "Examples")
        );
}

HtmlHelper 类位于 .NET Core 的 Microsoft.AspNetCore.Mvc.ViewFeatures 中,但 ActionLink 方法需要更多信息。它现在需要 8 个参数,而不是旧项目中的 3 个参数,其中两个是协议和主机名。但我不确定如何在不访问 HttpContext 的情况下在静态类中获取主机名和协议。

【问题讨论】:

  • IHtmlHelper 接口的 ViewContext 属性具有您可以检查的 HttpContext 属性。
  • 你确定吗?根据文档,应该有一个只需要 3 个参数的重载:docs.microsoft.com/en-us/dotnet/api/…

标签: c# html-helper asp.net-core-2.2


【解决方案1】:

在 ASP.NET Core 中,以前称为 HtmlHelper 的类现在已替换为接口 IHtmlHelper

这意味着所有链接扩展(HtmlHelperLinkExtensions)也已切换到界面。

https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.rendering.htmlhelperlinkextensions?view=aspnetcore-2.2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-19
    • 2010-10-05
    • 2023-02-14
    • 2016-08-17
    • 2014-12-28
    • 1970-01-01
    • 2014-11-23
    相关资源
    最近更新 更多