我需要一个html Helper中的url helper,所以我有
IHtmlHelper<T> html
并且需要获得IUrlHelper
我试过了:
html.ViewContext.HttpContext.RequestServices.GetService(typeof(IUrlHelper));
但它返回null
答案 0 :(得分:2)
得到了答案: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);