// POST api/Contacts public HttpResponseMessage PostContact(Contact contact) { if (ModelState.IsValid) { db.Contacts.Add(contact); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, contact); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = contact.ContactId })); return response; } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }
// PUT api/Contacts/5 public HttpResponseMessage PutContact(int id, Contact contact) { if (ModelState.IsValid && id == contact.ContactId) { db.Entry(contact).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return Request.CreateResponse(HttpStatusCode.NotFound); } return Request.CreateResponse(HttpStatusCode.OK); } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }