Injection of IUrlHelper in ASP.NET Core

后端 未结 6 1925
借酒劲吻你
借酒劲吻你 2020-12-05 17:29

In RC1, IUrlHelper could be injected in services (with services.AddMvc() in startup class)

This doesn\'t work anymore in <

相关标签:
6条回答
  • 2020-12-05 17:39

    For ASP.NET Core 3.x app just inject IHttpContextAccessor and LinkGenerator to your controller or service. They should be already available in DI.

    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Routing;
    
    namespace Coding-Machine.NET
    {
        public class MyService
        {
            private readonly IHttpContextAccessor _accessor;
            private readonly LinkGenerator _generator;
    
            public MyService(IHttpContextAccessor accessor, LinkGenerator generator)
            {
                _accessor = accessor;
                _generator = generator;
            }
    
            private string GenerateConfirmEmailLink()
            {
                var callbackLink = _generator.GetUriByPage(_accessor.HttpContext,
                    page: "/Account/ConfirmEmail",
                    handler: null, 
                    values: new {area = "Identity", userId = 123, code = "ASDF1234"});
    
                return callbackLink;
            }
        }
    }
    

    If your app can't resolve IHttpContextAccessor just add this to DI:

    public void ConfigureServices(IServiceCollection services)
    {
         services.AddHttpContextAccessor();
    }
    
    0 讨论(0)
  • 2020-12-05 17:44

    For ASP.Net Core 2.0 you must not inject an IUrlHelper. It’s available as a property of the controller. ControllerBase.Url is an IUrlHelper instance.

    0 讨论(0)
  • 2020-12-05 17:46

    For .Net Core 2.0

    services.AddMvc();
    
    services.AddScoped<IUrlHelper>(x =>
    {
       var actionContext = x.GetRequiredService<IActionContextAccessor>().ActionContext;
       var factory = x.GetRequiredService<IUrlHelperFactory>();
       return factory.GetUrlHelper(actionContext);
    });
    
    0 讨论(0)
  • 2020-12-05 17:49

    For ASP.NET Core RC2 there is an issue for this on the github repo. Instead of injecting the IUrlHelper, take an IUrlHelperFactory. It also sounds like you'd need the IActionContextAccessor injected as a Controller no longer has a public property ActionContext.

    Register the dependency:

    services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
    

    Then depend on it:

    public SomeService(IUrlHelperFactory urlHelperFactory,
                       IActionContextAccessor actionContextAccessor)
    {
    
        var urlHelper =
            urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext);
    }
    

    Then use it as you see fit.

    0 讨论(0)
  • 2020-12-05 17:57

    For Net Core 2.0

    Add this after service.AddMvc()

    services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
    services.AddScoped<IUrlHelper>(factory =>
    {
        var actionContext = factory.GetService<IActionContextAccessor>()
                                       .ActionContext;
        return new UrlHelper(actionContext);
    });
    
    0 讨论(0)
  • 2020-12-05 17:58

    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

    0 讨论(0)
提交回复
热议问题