LinkGenerator in ASP.NET Core 2.2 (Updated)

October 15, 2018
No Comments.

As ASP.NET Core 2.2 is now in preview, i’ve been looking at some of the early features for an update to one of my Pluralsight courses. ASP.NET Core 2.2 includes a number of new features, but this is a feature I really like.

If you haven’t seen the whole list of new ASP.NET Core 2.2 features (in Preview 3 as of the updating of this post), take a look at the announcement here:


UPDATE: 2.2 Preview 3 Changes this behavior

LinkGenerator is a new service that handles generating links. 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 being called in a controller or other reference to the request.

To use it, you’d just inject it into your code (a controller in this case):

  public class CampsController : ControllerBase
    protected readonly LinkGenerator _linkGenerator;

    public CampsController(..., LinkGenerator linkGenerator)
      _linkGenerator = linkGenerator;


An obvious use-case for this is when you implement a POST in an API. UrlHelper has been used to do this before, but it required we name each action we needed to use. LinkGenerator is different because, you can generate a link by just supplying the action and route values. You can specify controller name if not the current controller. For example:

    public async Task<ActionResult<CampModel>> Post(CampModel model)

      var link = _linkGenerator.GetPathByAction(
        values: new { moniker = camp.Moniker });

      return Created(link, camp);


While supplying the route values works, you can still use action names if you like. For me, the real benefit of this new service is that it can just be injected wherever you need it, not just into places where the request or response is necessary.

Take a look at this new service and see how you’d use it once 2.2 is released. Here is a short article (from Preview 1) that talks about the LinkGenerator if you want to see how Microsoft is positioning the new service:

What do you think?