LinkGenerator in ASP.NET Core

January 22, 2019 by Anuraj

ASP.NET Core

This post is about LinkGenerator in ASP.NET Core. Link Generator Service is a new service introduced in ASP.NET Core 2.2, which helps you to generate URLs. This new service can be used from middleware, and does not require an HttpContext.

You can use it in controller like this.

private readonly LinkGenerator _linkGenerator;
public HomeController(LinkGenerator linkGenerator)
{
    _linkGenerator = linkGenerator;
}
public IActionResult Index()
{
    var url = _linkGenerator.GetPathByAction("Privacy", "Home");
    return View();
}

And you can use similar way in middleware as well. Unlike UriHelper, in LinkGenerator you can generate a link by just supplying the action and route values. You can specify controller name if not the current controller. For right now the set of things you can link to is limited to MVC actions, but this will expand in 3.0.

Happy Programming :)

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub