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