zoukankan      html  css  js  c++  java
  • Winform简单调用WebApi

    WebAPI  Controllers

       public class SimuController : ApiController
        {     
            //EF 5
            BIM_GENERALDICTONARY_DBEntities entities=new BIM_GENERALDICTONARY_DBEntities(); 
            
            // GET api/Simu
            public IEnumerable<T_BIM_PropityClass> Get()
            {
                return entities.T_BIM_PropityClass;
            }
        }

    WinForm Client

         static readonly Uri _baseAddress = new Uri("http://localhost:5373/");
            static readonly Uri _address = new Uri(_baseAddress, "/api/simu");
            private void button1_Click(object sender, EventArgs e)
            {
    
                WebClient webClient = new WebClient();
                webClient.Headers["Accept"] = "application/json";
                webClient.Encoding = Encoding.UTF8;webClient.DownloadStringCompleted += (send, es) =>
                    {
                     if (es.Result != null)
                     {
                         var test = JsonConvert.DeserializeObject<T_BIM_PropityClass[]>(es.Result);
                         if (test.Any())
                         {
                             gridControl1.DataSource = test;}
                     }
                     else
                     {
                         MessageBox.Show(es.Error.Message);
                     }
                    };
                webClient.DownloadStringAsync(_address);
    
            }
    //将Json字符串转换为 对像   
    public static T Deserialize<T>(string json) { JavaScriptSerializer Serializer = new JavaScriptSerializer(); T objs = Serializer.Deserialize<T>(json); return objs; }
  • 相关阅读:
    2019 年百度之星—初赛一 B题 Game
    蒲公英
    集训队8月17日
    CodeForces 550E Brackets in Implications 推理
    11076: 小P的集合 位运算
    树状数组区间修改版本模板
    Contest1692
    POJ
    树状数组进阶-区间修改+区间查询
    具体数学-第二章-和式
  • 原文地址:https://www.cnblogs.com/w2011/p/3913765.html
Copyright © 2011-2022 走看看