// PUT api/Task/5 public HttpResponseMessage PutTask(int id, Task task) { if (ModelState.IsValid && id == task.ID) { db.Entry(task).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return Request.CreateResponse(HttpStatusCode.NotFound); } return Request.CreateResponse(HttpStatusCode.OK); } return Request.CreateResponse(HttpStatusCode.BadRequest); }
// POST api/Task public HttpResponseMessage PostTask(Task task) { if (ModelState.IsValid) { if (!db.Tasks.Any()) { task.ID = 0; } else { task.ID = db.Tasks.Max(record => record.ID) + 1; } db.Tasks.Add(task); db.SaveChanges(); db.Configuration.ProxyCreationEnabled = false; var response = Request.CreateResponse(HttpStatusCode.Created, task); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = task.ID })); return response; } return Request.CreateResponse(HttpStatusCode.BadRequest); }