From the course: Building and Securing RESTful APIs in ASP.NET Core

Create a Link class - ASP.NET Core Tutorial

From the course: Building and Securing RESTful APIs in ASP.NET Core

Start my 1-month free trial

Create a Link class

- [Instructor] Earlier, we used the URL.link method in a controller to generate an absolute URL to a resource. This method isn't available outside of the controller, so we'll need to find an alternate way for our service code to handle links between resources. As a first step, let's create a simple model of a link. The Ion specification describes three parts of a hypermedia link. An href, a method, and one or more relations. Only the href is actually required. The others are optional here. Let's build a model that represents this structure in our project. In the models folder, I'll create a class called link, and add some properties. We'll add a href. And a string array for relations. And a string for method. I can add some json.net properties here to control how this is serialized. I'll use the JsonProperty attribute. And say that this is order equals negative four to put this at the very top. Here we'll say order equals negative three and also serialize the property name as rel instead of relations. If it's null, we'll just say we wanna ignore it. Finally for method, we'll say that this is order negative two, say that if it's either default value or the null value, we'll ignore it. We need to give this a default value, and according to the IL spec, if the method is blank or null, it's assumed to be get. So we'll set default value, this comes from the component model namespace, default value to get. Why don't we just move this up to a constant, so we don't use a stream literal there. We'll say public const string getmethod, we'll say that's the value get and use that here. That takes care of the properties that need to be returned to the client for a link, but we still haven't solved the problem of generating an absolute URL from our service code. What we do have in our service code and mapping code is the name of the route and any route parameters. These need to be passed to URL link at some point to generate the absolute URI. What we'll do is we'll stash these values in the link class and then create a filter that calls URL.link right before the response is sent to the client. That means we need to add two more properties here. We'll say public string routename and public string routevalues. Now these are only temporary. We're going to use JsonIgnore so they don't get actually serialized when we send this back to the client. We'll just say stores the route name before being rewritten by the link rewriting filter. To make creating a link instance really simple for the controller and service code, let's add a helper function here that sets all the default values. We'll do this at the top. I'll say public static link to and that takes a string, route name and optionally some route values. But it's an optional parameter. And here we're just gonna build up a link object. New link, route name equals the route name same with the route values, the method is gonna be the get method and relations will be null. I should make this an object. Route values should be an object, not a string. There we go. Now we can use this link class in our controller and service code. I've refactored the root controller slightly to return a model called root response. Now we can replace these calls to URL.link with link.to for the rooms and info links. So let's say link.to, name of roomscontroller.getrooms. Now we don't need any route parameters there so we can just leave the rest off. And for this one, grab this and then say link.to, nameof infocontroller.getinfo. Next, we'll write that filter that will automatically generate an absolute URL to place in the href property.

Contents