How to implement search feature in ASP.NET Core REST API.
Code Explanation
As per the [Route] attribute on the EmployeesController, the route to reach this controller is /api/employees
The following [HttpGet] attribute specified an extension to the base route /api/employees. So the route to reach this Search() method is /api/employees/search. The values for the two method parameters come from the query strings in the URL.
If you want the search terms (name and gender) to be included as route parameters instead of query strings, change the route template on the HttpGet attribute as shown below. However, this approach is not recommended if you have many search parameters.
Since the route parameters and method parameters are mapped by name, even if the order does not match, they will still be correctly mapped i.e the URL parameter name is mapped to the method parameter name and the gender URL parameter is mapped to the gender method parameter.
No comments:
Post a Comment