In this video, Nate shows you how to reuse ION Forms to make querying the API with sort and search more discoverable.
- [Instructor] In earlier chapters, we created a sort and search syntax that could be used to query collections. The sort and search parameters are sent to the collection as get query string parameters. Like this. We might do /rooms?orderBy=rate, or maybe /rooms?search=name eq Oxford Room. Or any combination of these. This may not look like a form at first glance, but query string parameters in a GET, are a form just like parameters in a POST body are.
In order to describe the sort and search syntax, and make it discoverable for clients, we can model it as an ION form and attach the form to a collection response. It should look something like this. Here we have a form where the href is a link to the collection itself. The method instead of being POST is GET. And the rel is query-form. Each of the fields in the value array are parameters that we can submit to the form. The first field describes the search parameter which uses a regex pattern to describe which search terms are allowed.
And the second field is the order by parameter. Which describes which options are available for ordering on this particular route. Since this form applies to the rooms collection, it makes sense to return it on the collection response. That's in the model called Rooms Response. So we can add a new form, called Rooms Query, or whatever you want to name it, and when we return this object in the rooms controller, we need to add this form.
So in get rooms Async, after we build up the collection, we can say collection.RoomsQuery=. We'll use the same technique as we used in the previous video, utilizing the form metadata class to generate this metadata for us. We can call form metadata, import that from infrastructure, and say from resource. We'll give it the room resource because that's what this collection contains, and we also need to give it a link to the form. So we'll say link.toform, give it the name of the route which is get rooms Async.
There are no route values, but we want to specify link get method as the method. And query relation as the relation. Now let's hit the rooms endpoint with Postman and see what we get in the response. So we'll hit local host/rooms. And what we get back includes the query form that we just defined. So we have an optional order by parameter, and an optional search parameter that can be submitted as query strings to the form with a GET request.
By returning a form that describes these query parameters, we've improved the discoverability of the API and made it embrace the hadious constraint even further. This wraps up our look at modifying data and using ION forms. If you want an additional challenge, try adding a query form to the /rooms/openingsroute. You can also add a form to each opening item that's prefilled with the values needed to book that opening. I'll include my solution to this challenge in the next set of exercise files.
- 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.