// POST api/CategoryInfoAPI public HttpResponseMessage PostCategory(Category category) { if (ModelState.IsValid) { db.Categories.Add(category); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, category); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = category.CategoryId })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
// PUT api/CategoryInfoAPI/5 public HttpResponseMessage PutCategory(int id, Category category) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != category.CategoryId) { return Request.CreateResponse(HttpStatusCode.BadRequest); } db.Entry(category).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }