Webapi Code:
public class Valuescontroller:apicontroller {entities db=new entities (); Get api/values public ienumerable<person> get () {return db.
person; //Get API/VALUES/5 public person get (int id) {return db.
Person.firstordefault (o => o.id = ID); }//Post api/values public void POST ([Frombody]person value) {//People peple = (Peo
PLE) Jsonconvert.deserializeobject (value); if (db. Person.firstordefault (o => o.id = = value.id) = = null) {db.
Person.add (value); } db.
SaveChanges (); //Put api/values/5 public void put (int id, [Frombody]person value) {var query = d
B.person.firstordefault (o => o.id = ID); if (query!= null) {query. Name = value.
Name; } db. SAvechanges (); }//DELETE api/values/5 public void DELETE (int id) {var query = db.
Person.firstordefault (o => o.id = ID); if (query!= null) {db.
Person.remove (query); Db.
SaveChanges (); }
}
}
The basic code for the
WinForm:
Private Uri URL = new Uri ("Http://localhost:18827/api/values"); <summary>///Add///</summary> private async void Button1_Click (object sender, Eve
Ntargs e) {People people = new people (); People. Id = new Random ().
Next (100, 9999999); People. Name = "Li" + people.
Id;
String data = Jsonconvert.serializeobject (people); Create a DataContractJsonSerializer DataContractJsonSerializer that handles serialization serializer = new DataContractJsonSerializer (
typeof (People));
MemoryStream ms = new MemoryStream (); Serializer.
WriteObject (MS, people); Be sure to set the position Ms here.
Position = 0; Httpcontent content = new Streamcontent (ms)//convert MemoryStream to Httpcontent//Be sure to set header content.
Headers.contenttype = new System.Net.Http.Headers.MediaTypeHeaderValue ("Application/json");
HttpClient client = new HttpClient (); Post Method Httpresponsemessage response = Await client is issued by HttpClient.
Postasync (URL, content); if (response. Issuccessstatuscode) {} else {}}/// <summary>///Delete///</summary> private async void Button2_Click (object sender, Eventa
RGS e) {httpclient client = new HttpClient (); Issued by HttpClient Delete method Httpresponsemessage response = await client.
Deleteasync (URL + "/1"); if (response.
Issuccessstatuscode) {MessageBox.Show ("success"); }///modify Private async void Button3_Click (object sender, EventArgs e) {//Create a DataContractJsonSerializer DataContractJsonSerializer serializer = new DataContractJsonSerializer for processing serialization (TYPEO
F (People)); MemoryStream ms = NEW MemoryStream (); Writes the data to MemoryStream serializer.
WriteObject (MS, New people () {Id = 1, Name = "Hello ni"}); Be sure to set the position Ms here.
Position = 0; Httpcontent content = new Streamcontent (ms);//convert MemoryStream to httpcontent content.
Headers.contenttype = new System.Net.Http.Headers.MediaTypeHeaderValue ("Application/json");
HttpClient client = new HttpClient (); Issued by httpclient httpresponsemessage response = await client.
Putasync (URL + "/1", content); if (response.
Issuccessstatuscode) {MessageBox.Show ("success"); }///<summary>///get all///</summary> private void Button4_Click (
Object sender, EventArgs e) {using (WebClient client = new WebClient ()) { Client.
headers["Type"] = "get"; Client. headers["Accept"] = "appLication/json "; Client.
Encoding = Encoding.UTF8; Client. downloadstringcompleted + = (senderobj, es) => {if (es). Result!= null) {var test = jsonconvert.deserializeobject<people[]> (es.
result);
Datagridview1.datasource = test;
}
}; Client.
DownloadStringAsync (URL); }
}