private void submit_Click(object sender, System.EventArgs e) { AlumnoModel alumno = new AlumnoModel(textBox1.Text, textBox2.Text, textBox3.Text); string message = HttpApiController.AddAlumno(alumno).Result; //label4.Text = message; }
public static async Task <string> AddAlumno(AlumnoModel alumno) //post { string message; var alumJson = JsonConvert.SerializeObject(alumno); try { client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("aplication/json")); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "api/AlumnoAPI") { Content = new StringContent(alumJson, Encoding.UTF8, "application/json") }; await client.SendAsync(request); message = "Se ha guardado correctamente."; } catch (HttpRequestException e) { message = "Ha ocurrido un problema. " + e; throw e; } return(message); }
//https://www.c-sharpcorner.com/article/crud-Asp-Net-web-api-with-entity-framework-in-Asp-Net-mvc/ public static async Task <AlumnoModel> FindAsync(int id) { try { AlumnoModel findedObject = null; client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = await client.GetAsync("api/AlumnoAPI/" + id); if (response.IsSuccessStatusCode) { findedObject = JsonConvert.DeserializeObject <AlumnoModel>( await response.Content.ReadAsStringAsync()); return(findedObject); } else { return(findedObject); } } catch (Exception e) { throw e; } }