Injection of IUrlHelper in ASP.NET Core example
could not find an irouter associated with the actioncontext.
asp net core get host url
httpcontext does not contain a definition for 'current asp net core
mock<iurlhelper core url link
request url scheme .net core
.net core request scheme

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

This doesn't work anymore in RC2. Does anybody know how to do it in RC2 as just newing up a UrlHelper requires an ActionContext object. Don't know how to get that outside a controller.

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 =

Then use it as you see fit.

Injecting UrlHelper in ASP.NET Core MVC, Which version of Visual Studio or the dotnet SDK were you using? 2.2.401 In Core 2.2, a singleton service called LinkGenerator is  Injecting UrlHelper in ASP.NET Core MVC One of our APIs has a dynamic routing system that invokes a different handler based on attributes of the incoming HTTP request. Each of these handlers is responsible for building the API response which includes generating hypermedia links that describe the state and capabilities of the resource, for example:

For Net Core 2.0

Add this after service.AddMvc()

services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
services.AddScoped<IUrlHelper>(factory =>
    var actionContext = factory.GetService<IActionContextAccessor>()
    return new UrlHelper(actionContext);

Should use LinkGenerator instead of injecting IUrlHelper · Issue , Defines the contract for the helper to build URLs for ASP.NET MVC within an application. Action(IUrlHelper, String, String, Object, String, String) Generates a URL with an absolute path for an action method, which contains the specified action name, controller name, route values , protocol to use, and host name.

ASP.NET Core 2.0


PM> Install-Package AspNetCore.IServiceCollection.AddIUrlHelper


public void ConfigureServices(IServiceCollection services)

Disclaimer: author of this package

IUrlHelper Interface (Microsoft.AspNetCore.Mvc), Assembly: Microsoft.AspNetCore.Mvc.Core.dll. An implementation of IUrlHelper that contains methods to build URLs for ASP.NET MVC within an application. If you are running ASP.NET Core 2.2 or later, you can also use the LinkGenerator instead of the IUrlHelper inside your services which gives you an easier way to generate URLs compared to having to construct the helper through the IUrlHelperFactory.

For .Net Core 2.0


services.AddScoped<IUrlHelper>(x =>
   var actionContext = x.GetRequiredService<IActionContextAccessor>().ActionContext;
   var factory = x.GetRequiredService<IUrlHelperFactory>();
   return factory.GetUrlHelper(actionContext);

UrlHelper Class (Microsoft.AspNetCore.Mvc.Routing), NET Core route for an email but are in a separate process? Here's how to use UrlHelper outside of ASP.NET Core. DependencyInjection; public static IUrlHelper GetStaticUrlHelper(this IWebHost host, Uri baseUri). November 19, 2019 in ASP.NET Core .NET Core 3.0 Configuration Dependency Injection ~ 8 min read. In this post I describe one of the changes to Startup when moving from an ASP.NET Core 2.x app to .NET Core 3; you can not longer inject arbitrary services into the Startup constructor. In .NET Core 3.0 the ASP.NET Core 3.0 hosting infrastructure

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.

Generating links with UrlHelper outside of ASP.NET Core , In RC1, IUrlHelper could be injected in services (with services.AddMvc() in startup class). This doesn't work anymore in RC2. Does anybody  ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. For more information specific to dependency injection within MVC controllers, see Dependency injection into controllers in ASP.NET Core .

LinkGenerator in ASP.NET Core 2.2, This is different from the UrlHelper that has been used in ASP.NET MVC for a long time in that it is just an injectable service, not depending on  For ASP.NET Core 5 Onwards There are indications that you will be able to use LinkGenerator to create absolute URL's without the need to provide a HttpContext (This was the biggest downside of LinkGenerator and why IUrlHelper although more complex to setup using the solution below was easier to use) See "Make it easy to configure a host/scheme

ASP.NET MVC Core IUrlHelper: Convert Your UrlHelper Extensions , ASP.NET was completely rewritten from the ground up. Things like everything being dependency injected, a new way to configure your  ASP.NET Core supports dependency injection into views. This can be useful for view-specific services, such as localization or data required only for populating view elements. You should try to maintain separation of concerns between your controllers and views. Most of the data your views display should be passed in from the controller.

Injecting IUrlHelper with Simple Injector. I'm working on an ASP.NET Core app using Simple Injector for dependency injection duties. I'm looking for a way to  In simple terms, SQL injection is nothing but it a technique where malicious users can inject SQL commands into an SQL statement, via webpage input and this input can break the security of the web application. Now we understand how SQL Injection can be done in ASP .NET websites. Let's take an example. Suppose you have a Login Table inside your

  • I'm not sure what to say, perhaps there is a bug - otherwise there might be something in the repo?
  • Added this code to my Startup.cs, injected IUrlHelper into my tag helper class as urlHelper. When I call urlHelper.Action() it throws IndexOutOfRangeException.
  • Disclaimer: You are the author of this package
  • I am trying to access IUrlHelper on a Hub in aspnet signalr core 2.1 will your package work?
  • @PhathutshedzoKhabubu package just registers IUrlHelper in ServiceCollection with necessary dependencies. As far as DI is available, IUrlHelper will also be available.
  • I installed this package in an asp,net core 2.0.6 application and tried injecting the IUrlHelper in a singnalR Core Hub but it was not injected and got back null so thats way I raised that and note I added services.AddUrlHelper(); after services.AddMvc()
  • I tried via constructor injection than I got an exception but Via IUrlHelperFactory the action context is null therefore it throws an exception that the argument can not be null
  • This doesn't answer the question. Often you don't want to take a dependency on a concrete controller in a class.
  • Agree with @Stephen . I want to inject the IUrlHelper into a Ui Service which has no dependency on a controller. I mean, why would it?
  • If I'm not mistaken the behavior of IUrlHelper depends on current Route and RouteData. In the case of direct injecting into Ui Service you may end up with surprising results.
  • @OleksandrDudnyk you're absolutely right. I've faced this "surprising" result. Is there any alternative for IUrlHelper to use correctly in a class?
  • What's your source on "you must not inject and IUrlHelper"?