c# - ASP.NET Core:HtmlHelper 扩展(迁移问题)
问题描述
我正在尝试更新旧 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 的情况下在静态类中获取主机名和协议。
解决方案
在 ASP.NET Core 中,之前调用的类HtmlHelper
现在已替换为 interface IHtmlHelper
。
这意味着所有链接扩展 ( HtmlHelperLinkExtensions
) 也已切换到接口。
推荐阅读
- python - 如何获得只有几个类的图像数据集的一小部分?
- db2 - 是否可以在 Merge 语句中实现循环并在每 6000 行中提交一次
- c# - WooCommerce webhook c# - 比较哈希
- ionic-framework - 在 appflow 中为 Web 进行生产构建
- vba - 在 word vba 中查找标题文本并选择下一个形状或内联形状
- java - 如何根据服务器端抛出的异常向客户端显示错误消息
- asp.net-mvc - 为什么交易不起作用?仅适用于第一个 .savechanges();
- ios - 将工作空间拆分为框架时如何处理扩展?
- javascript - 为什么 Applisten 在 sequelize 之前运行?
- c++ - 使用字符串变量作为文件路径从子目录获取文件 - C++