public IHttpActionResult PutPerson(int id, Person person) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != person.PersonID) { return(BadRequest()); } db.Entry(person).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!PersonExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public HttpResponseMessage Put(int id, [FromBody] Employee employee) { try { using (WebAPIDemoEntities entity = new WebAPIDemoEntities()) { var ent = entity.Employee.FirstOrDefault(e => e.ID == id); if (ent == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with id : " + id + " not found.")); } else { ent.FirstName = employee.FirstName; ent.LastName = employee.LastName; ent.Gender = employee.Gender; ent.Salary = employee.Salary; entity.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, ent)); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage Post([FromBody] Employee employee) { try { using (WebAPIDemoEntities entity = new WebAPIDemoEntities()) { entity.Employee.Add(employee); entity.SaveChanges(); var message = Request.CreateResponse(HttpStatusCode.Created, employee); message.Headers.Location = new Uri(Request.RequestUri + "/" + employee.ID.ToString()); return(message); } }catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage Delete(int id) { try { using (WebAPIDemoEntities entity = new WebAPIDemoEntities()) { var ent = entity.Employee.FirstOrDefault(e => e.ID == id); if (ent == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with id : " + id + " not found.")); } else { entity.Employee.Remove(ent); entity.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, ent)); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }