如何从 Html helper 内部获取 IUrlHelper

Omu*_*Omu 3 asp.net-core

我需要 html Helper 中的 url helper 所以我有

IHtmlHelper<T> html 并且需要得到 IUrlHelper

我试过这个:

html.ViewContext.HttpContext.RequestServices.GetService(typeof(IUrlHelper));
Run Code Online (Sandbox Code Playgroud)

但它返回空

Omu*_*Omu 7

在这里得到答案: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);
Run Code Online (Sandbox Code Playgroud)