public static Artist AddArtist(ArtistModel artist) { HttpResponseMessage response = client.PostAsXmlAsync("api/artists", artist).Result; var myArtist = response.Content.ReadAsAsync<Artist>().Result; if (response.IsSuccessStatusCode) { Console.WriteLine("Artist added: {0}", artist.Name); } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } return myArtist; }
// POST api/Artists public HttpResponseMessage PostArtist(ArtistModel artist) { if (artist == null) { var errResponse = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error!"); return errResponse; } Artist artistToAdd = artist.ToArtist(); var entity = this.repository.Add(artistToAdd); var response = this.Request.CreateResponse(HttpStatusCode.Created, entity); response.Headers.Location = new Uri(this.Request.RequestUri + artist.ArtistId.ToString(CultureInfo.InvariantCulture)); return response; }