// POST api/Product public HttpResponseMessage PostProduct(Product product) { if (ModelState.IsValid) { product.rowguid = Guid.NewGuid(); product.ModifiedDate = DateTime.UtcNow; db.Products.Add(product); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, product); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = product.ProductID })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
// PUT api/Product/5 public HttpResponseMessage PutProduct(int id, Product product) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != product.ProductID) { return Request.CreateResponse(HttpStatusCode.BadRequest); } product.ModifiedDate = DateTime.UtcNow; db.Entry(product).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }