// PUT api/Products/5 public HttpResponseMessage Put([FromODataUri] int key, Product product) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (key != product.Id) { return Request.CreateResponse(HttpStatusCode.BadRequest); } data.Entry(product).State = EntityState.Modified; try { data.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.NoContent); }
// POST api/Products public HttpResponseMessage Post(Product product) { if (ModelState.IsValid) { data.Products.Add(product); data.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, product); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = product.Id })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }