码迷,mamicode.com
首页 > Windows程序 > 详细

Winform简单调用WebApi

时间:2014-08-15 01:24:27      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:des   winform   style   blog   http   color   os   io   

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

        }

 

Winform简单调用WebApi,布布扣,bubuko.com

Winform简单调用WebApi

标签:des   winform   style   blog   http   color   os   io   

原文地址:http://www.cnblogs.com/w2011/p/3913765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!