[英]ASP.NET Core : HtmlHelper extension (migration issue)
I am trying to update some code in a old MVC-project (.NET Framework 4.5.2) to work with .NET Core 2.2.我正在尝试更新旧 MVC 项目(.NET Framework 4.5.2)中的一些代码以使用 .NET Core 2.2。 I got stuck on a extension method of HtmlHelper that is generating a link inside a string.
我被困在 HtmlHelper 的扩展方法上,该方法在字符串内生成链接。
public static HtmlString GetMenu(this HtmlHelper htmlHelper)
{
htmlString += string.Format("<li{0}>{1}</li>",
controller == "Examples" ? " class=\"selected\"" : "",
htmlHelper.ActionLink("Examples", "Index", "Examples")
);
}
The HtmlHelper class are found inside the Microsoft.AspNetCore.Mvc.ViewFeatures in .NET Core, but the method ActionLink want more information. HtmlHelper 类位于 .NET Core 中的 Microsoft.AspNetCore.Mvc.ViewFeatures 中,但方法 ActionLink 需要更多信息。 Instead of 3 arguments as in the old project, it now wants 8 arguments, and two of these are protocol and hostname.
与旧项目中的 3 个参数不同,它现在需要 8 个参数,其中两个是协议和主机名。 But I am not sure how to get hostname and protocol in a static class, without accessing the HttpContext.
但是我不确定如何在不访问 HttpContext 的情况下在静态类中获取主机名和协议。
In ASP.NET Core, the class that was previously called HtmlHelper
has now been replaced by the interface IHtmlHelper
.在 ASP.NET Core 中,以前称为
HtmlHelper
的类现在已被接口IHtmlHelper
取代。
This means that all the link extensions ( HtmlHelperLinkExtensions
) have switched to the interface as well.这意味着所有链接扩展(
HtmlHelperLinkExtensions
)也已切换到该界面。
https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.rendering.htmlhelperlinkextensions?view=aspnetcore-2.2 https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.rendering.htmlhelperlinkextensions?view=aspnetcore-2.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.