// PUT api/Employees/5 public HttpResponseMessage PutEmployee(int id, Employee employee) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != employee.Id) { return Request.CreateResponse(HttpStatusCode.BadRequest); } db.Entry(employee).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }
// POST api/Employees public HttpResponseMessage PostEmployee(Employee employee) { if (ModelState.IsValid) { db.Employees.Add(employee); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, employee); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = employee.Id })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }