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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With