Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC6IURLHelper依赖项注入_C#_Dependency Injection_Inversion Of Control_Asp.net Core Mvc_Urlhelper - Fatal编程技术网

C# MVC6IURLHelper依赖项注入

C# MVC6IURLHelper依赖项注入,c#,dependency-injection,inversion-of-control,asp.net-core-mvc,urlhelper,C#,Dependency Injection,Inversion Of Control,Asp.net Core Mvc,Urlhelper,我希望通过依赖项注入使用IUrlHelper,以便能够使用其功能为不同的rest端点生成URI。我似乎不知道如何从头开始创建UrlHelper,因为它在MVC6中发生了更改,MVC不会自动在IoC控制器中提供该服务 该设置是我的控制器采用的内部模型到api模型转换器类,它使用IUrlHelper(全程依赖注入) 如果有更好的替代IUrlHelper/UrlHelper的方法,我可以使用它为我的WebApi操作/控制器生成URI,我愿意接受建议。这种方法现在已经过时了。查看下面的更新。 您可以插入

我希望通过依赖项注入使用IUrlHelper,以便能够使用其功能为不同的rest端点生成URI。我似乎不知道如何从头开始创建UrlHelper,因为它在MVC6中发生了更改,MVC不会自动在IoC控制器中提供该服务

该设置是我的控制器采用的内部模型到api模型转换器类,它使用IUrlHelper(全程依赖注入)


如果有更好的替代IUrlHelper/UrlHelper的方法,我可以使用它为我的WebApi操作/控制器生成URI,我愿意接受建议。

这种方法现在已经过时了。查看下面的更新。

您可以插入IHttpContextAccessor并从中获取服务,而不是
services.AddTransient()
或尝试直接插入IUrlHelper

public ClassConstructor(IHttpContextAccessor contextAccessor)
{
    this.urlHelper = contextAccessor.HttpContext.RequestServices.GetRequiredService<IUrlHelper>();
}

UrlHelper需要当前的操作上下文,我们可以从ActionContextAccessor获取该上下文。我用这个:

        services.AddScoped<IActionContextAccessor, ActionContextAccessor>();
        services.AddScoped<IUrlHelper>(x =>
        {
            var  actionContext = x.GetService<IActionContextAccessor>().ActionContext;
            return new UrlHelper(actionContext);
        });
services.addScope();
services.AddScoped(x=>
{
var actionContext=x.GetService().actionContext;
返回新的UrlHelper(actionContext);
});

现在,您可以直接将IUrlHelper注入任何需要它的内容,而无需跳转到IHttpContextAccessor。

ASP.NET Core 2.0

安装

使用


免责声明:此.NET CORE 3.1软件包的作者

services.AddSingleton<IActionContextAccessor, ActionContextAccessor>()
                .AddScoped(x =>
                    x.GetRequiredService<IUrlHelperFactory>()
                        .GetUrlHelper(x.GetRequiredService<IActionContextAccessor>().ActionContext)); //Inject UrlHelp for
services.AddSingleton()
.addScope(x=>
x、 GetRequiredService()
.GetUrlHelper(x.GetRequiredService().ActionContext))//为…注入帮助

是否有一个选项可以包装现有的UrlHelper,例如IUrlWrapper,然后改用包装器?我需要UrlHelper仍然正确?那会有什么不同呢?我想不出一种方法你可以不避开一个助手。从DI的角度来看,正如我前面提到的,您需要创建一个包装器。我想我不明白。如果我不知道如何获取UrlHelper,我应该如何包装它。这很好,因为我需要将UrlHelper注入到控制器中的服务中,所以它需要在我进入控制器之前发生。很好的解决方案。这在.net core 2.2上运行,但是我不得不将
IActionContextAccessor
更改为
AddTransient
而不是
AddScoped
,因为使用了
IUrlHelper.RouteUrl
在我的消费类中没有找到支持服务的组件Microsoft.AspNetCore.Mvc.IUrlHelper'@tchelize这可能不再适用。这是在最初发布之前,现在有了AspnetCore2。不确定您使用的是什么版本的
.net core 1.1
。请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题。带有解释的答案通常更有帮助,质量更好,更容易吸引选票
        services.AddScoped<IActionContextAccessor, ActionContextAccessor>();
        services.AddScoped<IUrlHelper>(x =>
        {
            var  actionContext = x.GetService<IActionContextAccessor>().ActionContext;
            return new UrlHelper(actionContext);
        });
PM> Install-Package AspNetCore.IServiceCollection.AddIUrlHelper
public void ConfigureServices(IServiceCollection services)
{
   ... 
   services.AddUrlHelper();
   ... 
}
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>()
                .AddScoped(x =>
                    x.GetRequiredService<IUrlHelperFactory>()
                        .GetUrlHelper(x.GetRequiredService<IActionContextAccessor>().ActionContext)); //Inject UrlHelp for