简体   繁体   English

ASP.NET Core:HtmlHelper 扩展(迁移问题)

[英]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.

相关问题 在 ASP.net 核心 mvc 3.1 中的 HtmlHelper 扩展方法中使用 DataAnnotation 本地化程序 - Using DataAnnotation localizer in your extension method for HtmlHelper in ASP.net core mvc 3.1 如何在 asp.net 核心 HtmlHelper 扩展中使用 IDisposable 模式捕获内部内容 - How in asp.net core HtmlHelper extension with IDisposable pattern to catch inner content RazorPage 中默认 HtmlHelper 的扩展方法 (ASP.NET CORE) - Extension Method for Default HtmlHelper in RazorPage (ASP NET CORE) 在ASP.Net Core中创建初始迁移的问题 - Issue with creating initial migration in ASP.Net Core asp.net MVC4 HtmlHelper扩展和渲染功能 - asp.net MVC4 HtmlHelper Extension and Render Function 在ASP.NET MVC4应用程序中向HtmlHelper添加扩展方法 - Adding an extension method to HtmlHelper in asp.net mvc4 application ASP.NET MVC 3:UrlHelper / HtmlHelper上的扩展方法编码为HTML - ASP.NET MVC 3: Extension method on UrlHelper/HtmlHelper encodes to HTML ASP.NET 核心 MVC:自定义 HtmlHelper 添加部分 - ASP.NET Core MVC : custom HtmlHelper adding sections 从ASP.NET WebAPI到ASP.NET Core WebAPI的路由迁移,方法问题不明确 - Routing migration from ASP.NET WebAPI to ASP.NET Core WebAPI, ambiguous method issue ASP.NET Core和Entityframwork迁移错误 - Asp.net core and Entityframwork migration error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM