asp.net-core - 如何从 Html helper 内部获取 IUrlHelper

标签 asp.net-core

我需要 html Helper 中的 url helper 所以我有
IHtmlHelper<T> html并且需要获取IUrlHelper
我试过这个:

html.ViewContext.HttpContext.RequestServices.GetService(typeof(IUrlHelper));

但它返回空

最佳答案

在这里得到答案:https://github.com/aspnet/Mvc/issues/5051
所以它看起来像这样:

public static IHtmlContent MyHelper(this IHtmlHelper<T> html){
    var urlHelperFactory = (IUrlHelperFactory)html.ViewContext.HttpContext.RequestServices.GetService(typeof(IUrlHelperFactory));            
    var urlHelper = urlHelperFactory.GetUrlHelper(aweInfo.Html.ViewContext);

关于asp.net-core - 如何从 Html helper 内部获取 IUrlHelper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38485147/

相关文章:

azure - ASP.NET 5 RC1-Final 中不存在 UseWindowsAzureActiveDirectoryBearerAuthentication

azure - SignalR核心: is ARR affinity needed when web sockets are enabled and the Azure app service is scaled out to multiple instances?

azure - .NET Core 2.2 无法发布到 Azure

javascript - 使用标签 data-* 实现 Ajax 调用

azure - 当 Azure Web 应用程序迁移到 .Net 6 时,虚拟连接字符串停止工作

visual-studio - Docker Desktop for Windows + ASP.NET Core 错误

c# - 具有多种实现的 .NET Core 3.1 中的依赖注入(inject)

c# - RESTful API 反序列化错误

javascript - 我应该在哪里包含 View 组件的脚本?

c# - ASP.NET Core 托管的 Blazor 模板中的授权问题