This video is available to students only

Adding Filter Feature to the API

In this lesson, we're going to add filter feature to our API

Adding filter feature to the API#

We can now sort our results in an ascending or descending way. Another important feature can be filtering search results which can be on the basis of an instructor, a category, price range and so on. This will again use a criteria where we're using Where statement to filter the search results. Let's filter the results based on category in this lesson; we will add more options in the future.

Inside CoursesController, GetCourses method, let's pass another parameter to it which will be the category id. This will be optional so we can put a question mark after the int, and pass this to the CoursesWithCategoriesSpecification; this will show an error. We can go inside the class, and write it inside the parameter; this will use the criteria to filter results, so we can extend the base and check if there's any categoryId. If there's a value, we can use an expression to check the categoryId with the course's category Id.

API/Controllers/CoursesController.cs

Start a new discussion. All notification go to the author.