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