Learn how to build attributes to annotate collections and describe which fields can be used in search queries.
- Before we dive into building search functionality I wanted to point out that I updated the exercise files with my solution to the challenge posed at the end of the previous chapter. You can find the updated code in the begin state of the exercise files for this video. I made a number of changes, so if you've been following along throughout this course, I'd recommend opening the solution file from the exercise files and continuing from there. Alright, let's add search to collections. We can use the same approach of annotating the resource models with attributes that we used for building sort functionality.
Let's create a new attribute called searchable attribute. I'll create this in the infrastructure folder. This class will inherit from the base attribute class and we'll add some properties with attribute usage. We'll say that this attribute targets a property and that you can't use it twice on the same property. We'll add some more properties to the searchable attribute later. But for now, let's go annotate the room resource.
Let's make both name and rate searchable fields. Next let's create a model that will hold the values past into the controller from the query stream. This model will be called search options T, TEntity and like the sort options class it will be an Ivalidatable object. We'll import that name space and we need to implement the method as well.
We'll declare a string array property called search that will hold the parameters past in the query stream. And let's add one more method here which returns an Iqueryable of TEntity called Apply and it'll accept an Iqueryable of TEntity as well. For now, we'll just throw an exception. Now, let's go over to the room controller and look at the get rooms async method.
Let's tell this method to look in query string as well for the search options. We'll bind this to room, room entity and call it search options. We'll need to make sure to pass this search option's instance down to the service class and update the service interface. This will now accept the search options of room comma room entity of search options and we'll need to update the implementation as well.
We want to apply the search options before we apply the sort options. That way filtering the collection is done first before we start sorting it. Alright, now that those pieces are in place, let's tackle the validation code.
- 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 Core (2016)with 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.