在RC1中,IUrlHelper可以注入服务(services.AddMvc()在启动类中)
这在RC2中不再起作用.有没有人知道如何在RC2中做到这一点,因为刚刚UrlHelper需要一个ActionContext对象.不知道如何在控制器之外得到它.
我正在实现电子邮件发送模块,并且需要构建URL。我的意图是使用IUrlHelper,但由于它需要提供大量的依赖关系,因此我确实很难构建该对象。
目前,我正在执行此步骤:
ActionContext actionCtx = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());
_urlHelper = new UrlHelper(actionCtx);
Run Code Online (Sandbox Code Playgroud)
但是以这种方式构造时,urlHelper在尝试构建URL时仍然会失败,并出现异常。
例外是:
索引超出范围。必须为非负数并且小于集合的大小。参数名称:Microsoft.AspNetCore.Mvc.Routing.UrlHelper.get_Router()上System.Collections.Generic.List`1.get_Item(Int32 index)处System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument参数,ExceptionResource资源)处的索引
所以现在可能需要创建和设置IRouter实例,但是正如我所看到的,再次需要设置大量的deps
那么应该如何正确完成呢?