C#
using Newtonsoft.Json;
C#
public class Dati
{
public string Id { get; set; }
public string nome { get; set; }
public string cognome { get; set; }
public string email { get; set; }
public DateTime
data_nascita { get; set; }
}
C#
private void
CaricaDati()
{
//Richiamo il metodo get della Web API
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:5081/"); //Indirizzo della Web API
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
//Eseguo la chiamata al metodo Get
HttpResponseMessage response = client.GetAsync("api/daticontrollers").Result;
if (response.IsSuccessStatusCode)
{
var datiJson =
response.Content.ReadAsStringAsync().Result;
var listaDati = JsonConvert.DeserializeObject<List<Dati>>(datiJson);
DtgDati.DataSource = null;
DtgDati.DataSource = listaDati;
}
else
{
MessageBox.Show("Errore nella chiamata alla Web API: " + response.StatusCode);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
C#
private void Form1_Load(object sender, EventArgs e)
{
CaricaDati();
}
C#
private void BtnWebApiMetodoInsert_Click(object sender, EventArgs e)
{
//Richiamo il metodo web api per l'inserimento del dato
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:5081/"); //Indirizzo della Web API
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
//Eseguo
la chiamata al metodo post
var nuovoDato = new Dati
{
nome = TxtNome.Text,
cognome =
TxtCognome.Text,
email = $"{TxtNome.Text}{TxtCognome.Text}@dominio.it",
data_nascita
= DateTime.Now
};
var json = JsonConvert.SerializeObject(nuovoDato);
var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response =
client.PostAsync("api/daticontrollers", content).Result;
if (response.IsSuccessStatusCode)
{
response = client.GetAsync("api/daticontrollers").Result;
if (response.IsSuccessStatusCode)
{
CaricaDati();
}
else
{
MessageBox.Show("Errore nella chiamata alla Web API: " + response.StatusCode);
}
}
else
{
MessageBox.Show("Errore nella chiamata alla Web API: " + response.StatusCode);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
C#
private void BtnWebAPIMetodoDelete_Click(object sender, EventArgs e)
{
//Elimino un dato tramite la web api
try
{
if (DtgDati.SelectedRows.Count == 0)
{
MessageBox.Show("Selezionare una riga da eliminare");
return;
}
var selectedRow = DtgDati.SelectedRows[0];
var idSelezionato =
selectedRow.Cells[0].Value.ToString();
if (string.IsNullOrEmpty(idSelezionato))
{
MessageBox.Show("record non trovato");
return;
}
string id = idSelezionato;
using (var client = new HttpClient())
{
client.BaseAddress
= new Uri("http://localhost:5081/"); //Indirizzo della Web API
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
//Eseguo
la chiamata al metodo delete
HttpResponseMessage response = client.DeleteAsync($"api/daticontrollers/{id}").Result;
if (response.IsSuccessStatusCode)
{
response = client.GetAsync("api/daticontrollers").Result;
if (response.IsSuccessStatusCode)
{
CaricaDati();
}
else
{
MessageBox.Show("Errore nella chiamata alla Web API: " + response.StatusCode);
}
}
else
{
MessageBox.Show("Errore nella chiamata alla Web API: " + response.StatusCode);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
C#
private void BtnWebAPIMetodoUpdate_Click(object sender, EventArgs e)
{
//Aggiorno un dato tramite la web api
try
{
if (DtgDati.SelectedRows.Count == 0)
{
MessageBox.Show("Selezionare
una riga da aggiornare");
return;
}
var selectedRow = DtgDati.SelectedRows[0];
var idSelezionato = selectedRow.Cells[0].Value.ToString();
if (string.IsNullOrEmpty(idSelezionato))
{
MessageBox.Show("record non
trovato");
return;
}
string id = idSelezionato;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:5081/"); //Indirizzo della Web API
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
//Eseguo la chiamata al metodo put
var
datoAggiornato = new Dati
{
nome = TxtNome.Text,
cognome =
TxtCognome.Text,
email = $"{TxtNome.Text}{TxtCognome.Text}@dominio.it",
data_nascita
= DateTime.Now
};
var json = JsonConvert.SerializeObject(datoAggiornato);
var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PutAsync($"api/daticontrollers/{id}", content).Result;
if
(response.IsSuccessStatusCode)
{
response = client.GetAsync("api/daticontrollers").Result;
if (response.IsSuccessStatusCode)
{
CaricaDati();
}
else
{
MessageBox.Show("Errore nella chiamata alla Web API: " + response.StatusCode);
}
}
else
{
MessageBox.Show("Errore nella chiamata alla Web API: " + response.StatusCode);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Le opere pubblicate in questo blog sono sotto la licenza Creative Commons. Attribuzione- No commerciale e no derivate.