WebAPI Several Get Methods
We know that there can be more than one GET methods in Web-API.
In the default setup, two method templates are reseved to the API-controller:
// GET: api/Employee
[HttpGet]
public IEnumerable
Get()
{
.....
}
// GET: api/Employee/5
[HttpGet("{id}", Name = "Get")]
public Employee Get(int id)
{
.....
}
This is typically the display for "all" employees in this case, and one for one particular employee with certain id.
Imagine that you want to make a different query of employee table , let's say, returning
more than one employees, not all but a filtered query.
[HttpGet("getadvanced",Name ="getadvanced")]
public IEnumerable
GetAdvanced()
{
return Employees.Where(i=>i.FirstName=="Sofia" || i.FirstName=="Mickey").ToList();
}
So simply repeat the HttpGet clause from above([HttpGet("{id}", Name = "Get")]), namely the Name ="getadvanced"
Afterwards, you can perform the query , http://localhost:32107/api/employee/getadvanced and get selected employees shown.
[{"id":2,"firstName":"Sofia","lastName":"Holm","position":"Clerk","age":24},
{"id":5,"firstName":"Mickey","lastName":"Mouse","position":"MouseTrapper","age":57}]