// POST: api/Vehicles public IHttpActionResult Post([FromBody]VehicleAdd 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 = w.Vehicles.Add(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 }); // Use the factory constructor for the "add new" use case VehicleLinked result = new VehicleLinked (Mapper.Map<VehicleWithLink>(addedItem), addedItem.Id); return Created(uri, result); }
// GET: api/Vehicles/5 public IHttpActionResult Get(int? id) { // Fetch the object var fetchedObject = w.Vehicles.GetById(id.GetValueOrDefault()); // Continue? if (fetchedObject == null) { return NotFound(); } VehicleLinked result = new VehicleLinked (Mapper.Map<VehicleWithLink>(fetchedObject)); return Ok(result); }