// PUT api/Tasks/5 public HttpResponseMessage PutTask(Int32 id, MyTask task) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != task.Id) { return Request.CreateResponse(HttpStatusCode.BadRequest); } db.Entry(task).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }
// POST api/Tasks public HttpResponseMessage PostTask(MyTask task) { if (ModelState.IsValid) { db.Tasks.Add(task); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, task); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = task.Id })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }