4

I need the url helper inside an html Helper so I have

IHtmlHelper<T> html and need to get IUrlHelper

I tried this:

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

but it returns null

Omu
  • 69,856
  • 92
  • 277
  • 407

1 Answers1

9

got the answer here: https://github.com/aspnet/Mvc/issues/5051 so it looks like this:

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);
Omu
  • 69,856
  • 92
  • 277
  • 407