In this video, learn how to extend the rewriting filter to automatically rewrite self-referential links on resources.
- [Instructor] We've used a result filter to automatically add absolute link urls to our API responses, but the href's on the resources themselves need the same treatment. Right now, in the resource model, the base class includes a property called Href. Let's replace this with a link property called Self. Now we can set this property in controller code. For example, in the root controller, instead of Href, now I can say Self equals Link.To, say name of Getroot.
This also works in other places, like in the Automapper profile that we created earlier. In mapping profile we can finally get rid of this ToDo and say ForMember, the destination, Self, and we will map that from, we'll say Link.To, the route name will be name of Controllers.RoomsController.GetRoomById.
And we also need to pass route values here, that'll be a new anonymous object, roomId equals src.Id. And we can get rid of this comment. The link rewriting filter will handle this new Self property with no problems. However, we can take it one step further. In the ion spec you're allowed to omit the Self item and put those link properties directly on the resource object. If these link properties exist on the resource, they're interpreted as a self-referential link.
We can express this in the model by making all resources also be links. So we'll say that this inherits from Link. We'll hide the Self property from the response by adding JsonIgnore, and then we'll extend the link rewriting filter to treat this Self link as a special case. In the link rewriting filter, down here, right under SetObject, or SetValue rather, I'm gonna say, "Special handling "of the hidden Self property: "unwrap into the root object." And I'll say, if this particular Property.Name is currently the nameof Resource.Self, I'll do allProperties.SingleOrDefault where the property name is the nameof Resource.Href.
And we'll say SetValue, model, rewritten.Href. Now we need to do this for the Method and Relations properties. So where p.name equals Method, SetValue to rewritten.Method, and, once more, where the name is Relations, set it to rewritten.Relations.
Now the Self link will be used to fill in the root link properties on the resource class. Let's go try it out. We've got the Href property back now and it's automatically being rewritten by our filter. That wraps up our look at handling hypermedia links in ASP.NET Core. You should understand how to express links between resources, both in your code and in Json responses.
- What is RESTful design?
- Building a new API with ASP.NET Core
- Using HTTP methods
- Returning JSON
- Creating RESTful routing with templates
- Securing RESTful APIs with HTTPS
- Representing resources
- Representing links
- Representing collections
- Sorting and searching collections
- Building forms
- Adding caching to an ASP.NET Core API
- Configuring user authentication and authorization
Skill Level Intermediate
1. REST API Concepts
2. Build a Basic API
3. Versioning and Errors
4. Secure the API
5. Represent Resources
6. Represent Links
7. Represent Collections
Add pagination7m 37s
8. Sorting Collections
9. Searching Collections
10. Forms and Modifying Data
11. Caching and Compression
12. Authentication and Authorization
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.