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