// PUT api/ProductTypes/5 public HttpResponseMessage PutProductType(int id, ProductType producttype) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != producttype.Id) { return Request.CreateResponse(HttpStatusCode.BadRequest); } data.Entry(producttype).State = EntityState.Modified; try { data.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Integer) { return new UberContext().ProductTypes.Find(reader.Value); } ProductType type = new ProductType(); serializer.Populate(reader, type); return type; }
// POST api/ProductTypes public HttpResponseMessage PostProductType(ProductType producttype) { if (ModelState.IsValid) { data.ProductTypes.Add(producttype); data.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, producttype); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = producttype.Id })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }