Search⌘ K
AI Features

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:

  1. Add a search parameter to the GetQuestions method:

Node.js
[HttpGet]
public IEnumerable<QuestionGetManyResponse>
GetQuestions(string search)
{
var questions = _dataRepository.GetQuestions();
return questions;
}
  1. Put a breakpoint on the statement that gets the questions from the repository and press “F5” to run the app:

Model binding with no search query parameter
Model binding with no search query parameter

We’ll see that the search parameter is null. Press “F5” to let the app continue.

  1. With the breakpoint still in place, change the URL in the browser to end with ... ...