コード例 #1
0
        public Controllers.EmployeeBase AddEmployee(Controllers.EmployeeAdd newItem)
        {
            // Use the repository method to add a new employee
            var addedItem = m.Employees.AddNew(newItem);

            return(addedItem);
        }
コード例 #2
0
        // POST: api/Employees
        public IHttpActionResult Post([FromBody] EmployeeAdd 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)
            {
                // Attempt to add the new item
                var addedItem = m.Employees.AddNew(newItem);

                if (addedItem == null)
                {
                    return(BadRequest("Cannot add the object"));
                }
                else
                {
                    // HTTP 201 with the new object in the entity body

                    // Build the URI to the new object
                    Uri uri = new Uri(Url.Link("DefaultApi", new { id = addedItem.Id }));

                    // Create an object to be returned
                    EmployeeLinked emp = new EmployeeLinked(Mapper.Map <EmployeeWithLink>(addedItem), uri.AbsolutePath);

                    // Return the object
                    return(Created(uri, emp));
                }
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }