コード例 #1
0
        // POST api/vehicles
        public HttpResponseMessage Post(VehiclePublic vehicle)
        {
            if (ModelState.IsValid)
            {
                // Add the new object
                var v = r.AddNew(vehicle);

                // Can we continue?
                if (v==null)
                {
                    return Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
                }
                else
                {
                    // Build the response object
                    var response = Request.CreateResponse<VehicleFull>(HttpStatusCode.Created, v);

                    // Set the Location header
                    response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = v.Id }));

                    return response;
                }
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
            }
        }
コード例 #2
0
        // Add new
        public VehicleFull AddNew(VehiclePublic vehicle)
        {
            // Must validate the incoming BodyStyle
            OtherClassesAndEnums.BodyStyle bodyStyle;
            if (!Enum.TryParse(vehicle.BodyStyle, out bodyStyle)) return null;

            var v = ds.Vehicles.Add(Mapper.Map<Models.Vehicle>(vehicle));
            ds.SaveChanges();

            return Mapper.Map<ViewModels.VehicleFull>(v);
        }
コード例 #3
0
        // Add new
        public VehicleFull AddNew(VehiclePublic vehicle)
        {
            // Must validate the incoming BodyStyle
            OtherClassesAndEnums.BodyStyle bodyStyle;
            if (!Enum.TryParse(vehicle.BodyStyle, out bodyStyle))
            {
                return(null);
            }

            var v = ds.Vehicles.Add(Mapper.Map <Models.Vehicle>(vehicle));

            ds.SaveChanges();

            return(Mapper.Map <ViewModels.VehicleFull>(v));
        }