I created a basic application using a WebAPI by following an online tutorial. It works fine for what it does. The GET method retrieves a list from a db table and populates it in the front end web site. I am simply trying to filter that list by passing one or more parameters.
This the code from the EmployeeController.cs file. Much of the code is unchanged from that created from the template. (sorry if it is too much but I wanted to provide sufficient info)
Can I achieve my goal by modifying the "return db.Employees;" line, or do I need to something more involved?
Thank you for your help!
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using WebAPI.Models;
namespace WebAPI.Controllers
{
public class EmployeeController : ApiController
{
private DBModel db = new DBModel();
// GET: api/Employee
public IQueryable<Employee> GetEmployees()
{
//return db.Employees;
return db.Employees;
}
// PUT: api/Employee/5
[ResponseType(typeof(void))]
public IHttpActionResult PutEmployee(int id, Employee employee)
{
if (id != employee.EmployeeID)
{
return BadRequest();
}
db.Entry(employee).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!EmployeeExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/Employee
[ResponseType(typeof(Employee))]
public IHttpActionResult PostEmployee(Employee employee)
{
db.Employees.Add(employee);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = employee.EmployeeID }, employee);
}
// DELETE: api/Employee/5
[ResponseType(typeof(Employee))]
public IHttpActionResult DeleteEmployee(int id)
{
Employee employee = db.Employees.Find(id);
if (employee == null)
{
return NotFound();
}
db.Employees.Remove(employee);
db.SaveChanges();
return Ok(employee);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool EmployeeExists(int id)
{
return db.Employees.Count(e => e.EmployeeID == id) > 0;
}
}
}