Extending the GetQuestions Action Method for Searching
Explore how to enhance the GetQuestions action method in ASP.NET Core by adding search functionality. Understand model binding to process query parameters, implement conditional data retrieval, and create a new action method for fetching unanswered questions. This lesson helps you build efficient and flexible REST API endpoints for dynamic data access.
We don’t always want all of the questions to be returned in the api/questions endpoint. Recall that our frontend had a search feature that returned questions that matched the search criteria.
Steps to handle a search request
Let’s extend our GetQuestions method to handle a search request. To do that, follow these steps:
Add a search parameter to the
GetQuestionsmethod:
Put a breakpoint on the statement that gets the questions from the repository and press “F5” to run the app:
We’ll see that the search parameter is null. Press “F5” to let the app continue.
With the breakpoint still in place, change the URL in the browser to end with ... ...