// POST api/Products public IHttpActionResult PostProduct(Product product) { if (ModelState.IsValid) { db.Products.Add(product); db.SaveChanges(); var uri = new Uri( Url.Link( "DefaultApi", new { id = product.ID })); return Created(uri, product); } else { return BadRequest(ModelState); } }
// PUT api/Products/5 public IHttpActionResult PutProduct(int id, Product product) { if (ModelState.IsValid && id == product.ID) { db.Entry(product).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return NotFound(); } return Ok(product); } else { return BadRequest(ModelState); } }