我需要 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/