LinkGenerator in ASP.NET Core 2.2 (Updated)


ASP.NET Core 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: blogs.msdn.microsoft.com.

LinkGenerator

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:

    [HttpPost]
    public async Task<ActionResult<CampModel>> Post(CampModel model)
    {
      ...

      var link = _linkGenerator.GetPathByAction(
        HttpContext,
        "Get",
        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:

https://blogs.msdn.microsoft.com/webdev/2018/08/27/asp-net-core-2-2-0-preview1-endpoint-routing/

What do you think?


Bootstrap 4 is Here!

After a long development cycle, Bootstrap has been completely re-written to improve performance and be more consistent. Learn Bootstrap 4 now with my Wilder Minds course:

Enroll Today


Shawn
Shawn Wildermuth
Author, Teacher, and Coach




My Courses

Wilder Minds Training
Vue.js by Example (Now Available)
Bootstrap 4 by Example
Intro to Font Awesome 5 (Free Course)
Pluralsight
Less: Getting Started (Coupon Available)
Building a Web App with ASP.NET Core, MVC6, EF Core, Bootstrap and Angular (updated for 2.1)
Using Visual Studio Code for ASP.NET Core Projects
Implementing ASP.NET Web API
Web API Design

Application Name WilderBlog Environment Name Production
Application Ver v4.0.30319 Runtime Framework x86
App Path D:\home\site\wwwroot\ Runtime Version .NET Core 4.6.26919.02
Operating System Microsoft Windows 10.0.14393 Runtime Arch X86