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