In this video, Nate shows you 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 Hrefs on the resources themselves need the same treatment. If we open up the resource model, right now the resource base class includes a property called Href, which should contain the absolute URL. 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 url.link, we can say self equals Link.To(name of(GetRoot)) route. This also works in other places like in the auto mapper mapping profile. When we're mapping the room entity to the room resource, we can say ForMember the destination member, self and we'll map that from Link.to the name of the rooms controller, get room by IdAsync route and we'll need to pass the route values, which are an anonymous object with roomId equal to the source room Id.
The link rewritting filter will handle this new self property without any issues, however we can take it one step further. In the ion spec you're allowed to omit the self item and put the link properties directly on the resource object. If these link properties exist directly on the resource, they are interpreted as a self referential link. We can express this in our model by making all resources also be links. In the resource class I'll update this to inherit from link and then I'll make the self property ignored in the serialization.
We'll need to update the link rerouting filter to handle this self link as special case. So, in the link rewriting filter right below where we're setting the link property to the rewritten link. We'll need to do an extra check. This will be special case, where we want to check to see if the link property, name is the name of the self property. If so, we'll set the Href methods and relations property directly on the link object, which resource is inheriting from.
So, we'll say, allProperties.SingleorDefault where the property name is the name of Resource.Href and we'll set value of that to be the rewritten Href and we'll do the exact same thing for the method and relation properties. This will effectively unwrapped that self property and place those values directly on the resource.
Let's try it out. If we make another request with postman, we should see the Href is now directly included on the resource. It's being automatically rewritten by our filter. This wraps up our look at handling hyper media links in ASP.net core. You should understand how to express links between resources both in your code and in j-son responses.
- REST vs. RPC
- Using HTTP methods (aka verbs)
- Returning JSON
- Creating a new API project
- Building a root controller
- Routing to controllers with templates
- Requiring HTTPS for security
- Creating resources and data models
- Returning data and resources from a controller
- Representing links (HREFs)
- Representing collections
- Sorting and searching collections
- Creating forms
- Caching and compression
- Authentication and authorization for RESTful APIs
Skill Level Intermediate
Deploying ASP.NET Core Applications (2017)with Nate Barbettini57m 57s Intermediate
1. REST API Concepts
2. Building a Basic API
3. Securing the API
4. Representing Resources
5. Representing Links
6. Representing Collections
7. Sorting Collections
8. Searching Collections
9. Forms and Modifying Data
10. Caching and Compression
11. 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.