The basic operation of WinForm to the web Api increase and search

Source: Internet
Author: User

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); }
        }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.