LinkGenerator in ASP.NET Core

Posted by Anuraj on Tuesday, January 22, 2019 Reading time :1 minute

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 :)

What do you think? I would like to hear your thoughts, suggestions, and questions in the comments section below.



Did you like this article? Share it with your friends

Facebook Twitter Google+ LinkedIn Reddit StumbleUpon



Translate
Subscribe
BMC logoBuy me a coffee


Copyright © 2019 - Anuraj P. 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. Hosted with ❤ by GitHub