LinkGenerator in ASP.NET Core 2.2

As ASP.NET Core 2.2 is now available, 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.

LinkGenerator

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

?12345678public 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:

?1234567891011121314[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.

Added a new service called LinkGenerator, it is a singleton service that supports generating paths and absolute URIs both with and without an HttpContext. If you need to generate links in Middleware or somewhere outside of Razor then this new service will be useful to you. You can use it in Razor, but the existing APIs like Url.Action are already backed by the new service so you can continue to use those.

return _linkGenerator.GetPathByAction(
     httpContext,
     controller: "Home",
     action: "Index",
     values: new { id=42 });

For now this is useful to link to MVC actions and pages from outside of MVC. They will add additional features in the next release targeting non-MVC scenarios.

Razor in ASP.NET Core

Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML. It’s just that ASP.NET MVC has implemented a view engine that allows us to use Razor inside of an MVC application to produce HTML.

You will have a template file that’s a mix of some literal text and some blocks of code. You combine that template with some data or a specific model where the template specifies where the data is supposed to appear, and then you execute the template to generate your output.

Microsoft wanted Razor to be easy to use and easy to learn, and work inside of tools like Visual Studio so that IntelliSense is available, the debugger is available, but they wanted Razor to have no ties to a specific technology, like ASP.NET or ASP.NET MVC.

If you’re familiar with the life cycle of an ASPX file, then you’re probably aware that there’s a dependency on the ASP.NET runtime to be available to parse and execute those ASPX files. Microsoft wanted Razor to be smart, to make a developer’s job easier.

Top and Reliable ASP.NET Core 2.2 Hosting Recommendation Discount 15% Off

ASPHostPortal as top and reliable ASP.NET Core hosting provider. ASPHostPortal.com is Microsoft No #1 Recommended Windows and ASP.NET Spotlight Hosting Partner in the United States. Microsoft presents this award to ASPHostPortal for the ability to support the latest Microsoft and ASP.NET technology. Use ASPHostPortal.com’s website building tools to get that special, customized look for your website. A nifty wizard will walk you through the process. Other companies promise cheap hosting, but then charge extra for setup fees, higher renewal rates, or promotional services.

With ASPHostPortal.com, the listed price is the number you’ll pay, and you can expect a fully loaded, comprehensive suite of web services. Their powerful servers are especially optimized and ensure the best ASP.NET Core performance. They have the best data centers on three continent and unique account isolation for security. ASPHostPortal.com webspace explorer lets you manage your website files with a browser. A control panel lets you set up and control your server functions with ease.

Posted in Hosting Article.