// This is the return type of the request. The method name is irrelevant as far as the clients are concerned. public ActionResult <object> PersonID_GET_URL(int id) { ActionResult <object> response; // This is what we are returning. It gets serialized as JSON if we return an object. try { Person person = new PersonControllerBLL().GetPersonByID(id); // This is "kind of" a DTO. We're putting the fields we care about into another object that is not the database model. // They help get around errors like the circular references, and (if you use them in the context) the missing virtual properties. response = new { id = person.ID, firstName = person.FirstName, lastName = person.LastName, phoneNumbers = person.PhoneNumbers.Select(x => x.Number) }; } catch { response = NotFound(new { error = $"No person at ID {id} could be found." }); } return(response); }
public ActionResult CreatePerson_POST(string firstName, string lastName, string phone) { ActionResult response; try { int newID = new PersonControllerBLL().CreatePerson(firstName, lastName, phone); // Just for fun: // (It's also an example of how to throw a code that doesn't have a method built-in) if (firstName.Trim().ToUpper() == "TEAPOT" && lastName.Trim().ToUpper() == "COFFEE") { response = StatusCode(418, new { message = $"Successfully created teapot but it does not want to brew coffee. It has the phone number {phone}." }); } else { // This should really be a Create() that provides the API endpoint for the GET to retrieve the created object. response = Created($"API/PersonAPI/People/ID/{newID}", new { message = $"Successfully created person {firstName} {lastName} with the phone number {phone} at ID {newID}." }); } } catch (PersonValidationException e) { response = UnprocessableEntity(new { errors = e.SubExceptions.Select(x => x.Message) }); } return(response); }