My team has currently implemented a REST API (JSON) using the Web API 2 platform for .NET. We have some working URLs, such as:
/api/schools
/api/schools/5000
/api/occupations
/api/occupations/22
And here is some of our data controller code:
public class OccupationsController : ApiController
{
// /api/Occupations/1991
public IHttpActionResult GetOccupation(int id)
{
var occupation = GetAllOccupations().FirstOrDefault((p) => p.OccupationID == id);
if (occupation == null)
{
return NotFound();
}
return Ok(occupation);
}
// /api/occupations
public IEnumerable<Occupation> GetAllOccupations()
{
var ctx = new TCOSDBEntities();
return ctx.Occupations.OrderBy(occupation => occupation.Title);
}
}
We are now introducing data filtering (based on user checkbox selection) and I am curious how to approach this in our existing API, or if I should abandon REST for filtering down and try a different approach all together?
Here is our checkbox filtering mechanism: Checkbox UI
How can I introduce search parameters in my REST services and DataController methods? Like, how do I get a range filter on a field (like Cost?)? Can I have multiple fields filtering, like Cost, Tuition, etc?