// POST api/<controller> public HttpResponseMessage Post([FromBody]Contact value) { using (var db = new AddressBookEntities()) { var contact = db.Contacts.Add(value); db.SaveChanges(); var response = Request.CreateResponse(HttpStatusCode.Created, contact); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = contact.ContactId })); return response; } }
// DELETE api/<controller>/5 public void Delete(int id) { using (var db = new AddressBookEntities()) { var contact = db.Contacts.Find(id); // Remove existing contact if (contact != null) { db.Contacts.Remove(contact); db.SaveChanges(); } } }
// PUT api/<controller>/5 public void Put(int id, [FromBody]Contact value) { using (var db = new AddressBookEntities()) { var contact = db.Contacts.Find(id); // Update existing contact if (contact != null) { contact.FirstName = value.FirstName; contact.MiddleName = value.MiddleName; contact.LastName = value.LastName; contact.Suffix = value.Suffix; contact.DOB = value.DOB; db.SaveChanges(); } } }