Learn how to build attributes to annotate collections and describe which fields are sortable.
- [Instructor] We'll add sort functionality to the API in a couple of steps. First let's extend the models and controllers to make them sortable. I'll start with the room model. I'm going to add a new attribute called sortable here to each one of the room model properties. This attribute doesn't exist yet so now we need to go create it. I'm going to create this in infrastructure folder. And name this sortable attribute. This attribute will inherit from the base attribute class. And we can set some attribute options with attribute usage.
We'll say that this targets a property and that you can't have more than one on each property. All right, next, let's create a model that will hold the query string arguments passed into the controller. I'll name this new model sort options of T T entity. I'll explain what this means in a minute. I'm going to declare one property which is a string array called order by. This will be bound to any order by parameters in the query string by ASP.net.
Declaring it as a string array means that multiple parameters can be passed in the query string. If we make this class inherit from the I validatable object interface, ASP.net core will be able to validate the parameters passed into our controller automatically. ASP.net core will call this validate method when it needs to check whether the parameters are valid. We'll create one more method here on sort options to use later. This is going to be an IQueryable of T entity called apply.
Which also takes an IQueryable of T entity. For now we'll just have this throw an exception 'cause we're not ready to implement it yet. This method will be used to apply the sort options passed into the controller dynamically to the query we'll send to the database. Let's go over to the rooms controller. If you look at the get rooms async method, you can see that I've updated it with my solution to the challenge I posed at the end of the previous chapter. I've added pagination to this method. Now we can extend it even further by adding the new sort options parameter we just defined.
Now we need to make sure that sort options gets passed into our service call. So we'll pass it to the get rooms async method on the service. And that means we'll have to go update the service interface as well. In the I rooms service interface, we'll accept a sort options of room room entity. Called sort options. Now let's update the interface implementation. This now accepts a sort options of room comma room entity.
Now we can apply the sort options that came from the controller to the query that we're sending to the database. We can say query equals sort options dot apply query. This works because the apply method takes an IQueryable and returns an IQueryable. We now have everything hooked up. But any incoming request will fail because the validation code throws an exception. We'll flush out this validation code next.
- 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
Building Web APIs with ASP.NET Corewith Chris Woodruff1h 7m Intermediate
Deploying ASP.NET Core Applicationswith 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.