// PUT api/Person/5 public HttpResponseMessage PutPerson(int id, Person person) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != person.Id) { return Request.CreateResponse(HttpStatusCode.BadRequest); } db.Entry(person).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }
// PUT api/Person/5 public async Task<IHttpActionResult> PutPerson(int id, Person person) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != person.Id) { return BadRequest(); } person.Place = null; db.Entry(person).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PersonExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> PostPerson(Person person) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.People.Add(person); await db.SaveChangesAsync(); return CreatedAtRoute("DefaultApi", new { id = person.Id }, person); }
// POST api/Person public HttpResponseMessage PostPerson(Person person) { if (ModelState.IsValid) { db.People.Add(person); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, person); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = person.Id })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }