// POST: api/Notes public IHttpActionResult Post([FromBody] NoteAdd newItem) { // Ensure that the URI is clean (and does not have an id parameter) if (Request.GetRouteData().Values["id"] != null) { return(BadRequest("Invalid request URI")); } // Ensure that a "newItem" is in the entity body if (newItem == null) { return(BadRequest("Must send an entity body with the request")); } // Ensure that we can use the incoming data if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Attempt to add the new object var addedItem = m.NoteAdd(newItem); // Continue? if (addedItem == null) { return(BadRequest("Cannot add the object")); } // HTTP 201 with the new object in the entity body // Notice how to create the URI for the Location header var uri = Url.Link("DefaultApi", new { id = addedItem.Id }); return(Created(uri, addedItem)); }
public NoteBase NoteAdd(NoteAdd newItem) { // Attention - 5 - Add new is NOT restricted var addedItem = ds.Notes.Add(Mapper.Map <Note>(newItem)); // Assign the owner addedItem.Owner = User.Name; ds.SaveChanges(); return((addedItem == null) ? null : Mapper.Map <NoteBase>(addedItem)); }