MVC 6 IUrlHelper Dependency Injection

好久不见. 提交于 2019-12-22 03:24:56

问题


I want to use IUrlHelper through dependency injection to be able to use its functionality to generate uris for different rest endpoints. I cant seem how to figure out how to create a UrlHelper from scratch because it changed in MVC 6 and MVC doesnt automatically have that service available in the IoC controller.

The setup is my Controller take in an internal model to api model converter class and that uses the IUrlHelper (all through Depenedency Injection).

If there is a better alternative to IUrlHelper/UrlHelper I can use to generate Uris for my WebApi action/controllers I am open to suggestion.


回答1:


The UrlHelper requires the current action context, and we can acquire that from the ActionContextAccessor. I'm using this:

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

Now, you can inject IUrlHelper directly into anything that needs it without having to jump through IHttpContextAccessor .




回答2:


This method is now obsolete. Look at update below.

Instead of services.AddTransient<IUrlHelper, UrlHelper>() or trying to directly inject IUrlHelper you can inject IHttpContextAccessor and get the service from there.

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

Unless it is just a bug, adding the IUrlHelper service with UrlHelper does not work.

UPDATE 2017-08-28

The previous method no longer seems to work. Below is a new solution.

Confgure IActionContextAccessor as a service:

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddSingleton<IActionContextAccessor, ActionContextAccessor>()
        .AddMvc();
}

Then inject IActionContextAccessor and IUrlHelperFactory to then generate the IUrlHelper like below

public class MainController : Controller
{
    private IUrlHelperFactory urlHelperFactory { get; }
    private IActionContextAccessor accessor { get; }
    public MainController(IUrlHelperFactory urlHelper, IActionContextAccessor accessor)
    {
        this.urlHelperFactory = urlHelper;
        this.accessor = accessor;
    }

    [HttpGet]
    public IActionResult Index()
    {
        ActionContext context = this.accessor.ActionContext;
        IUrlHelper urlHelper = this.urlHelperFactory.GetUrlHelper(context);
        //Use urlHelper here
        return this.Ok();
    }
}



回答3:


ASP.NET Core 2.0

Install

PM> Install-Package AspNetCore.IServiceCollection.AddIUrlHelper

Use

public void ConfigureServices(IServiceCollection services)
{
   ... 
   services.AddUrlHelper();
   ... 
}

Disclaimer: author of this package



来源:https://stackoverflow.com/questions/31059497/mvc-6-iurlhelper-dependency-injection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!