您是第欢迎光临我的主页位访客
浩凡儿
天行健,君子以自强不息;地势坤,君子以厚德载物!


====》两种网络服务
1.WebService:基于SOAP风格的网络服务,使用方法进行请求
2.WebAPI :基于REST风格的网络服务,使用资源进行请求;5个方法:查一个,查所有,增加,修改,删除就是定义一个控制器继承于WebAPIControler类

 public class UserInfoController : ApiController
    {
        // GET api/userinfo
        public IEnumerable<UserInfo> Get()
        {
            List<UserInfo> lUsers = new List<UserInfo>();
            lUsers.Add(new UserInfo() 
                { 
                    ID=1,
                    Name="孙宝"
                });
            lUsers.Add(new UserInfo() 
            { 
                ID = 2,
                Name = "王沙"
            });
            lUsers.Add(new UserInfo() 
            { 
                ID = 3,
                Name = "王闯" 
            });
            return lUsers;
        }

        // GET api/userinfo/5
        [HttpGet]
        public string Get(int id)
        {
            return "value";
        }

        // POST api/userinfo
        [HttpPost]
        public void Post([FromBody]string value)
        {
        }

        // PUT api/userinfo/5
        [HttpPut]
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/userinfo/5
        [HttpDelete]
        public void Delete(int id)
        {
        }
    }

 


====》使用 1.jquery 的ajax (缺点,不能跨域) 

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="Scripts/jquery-1.8.2.min.js"></script>
    <script type="text/javascript">
        $(function () {
            LoadList();
        });

        function LoadList() {
            $.ajax({
                type: 'get',//请求方式,可以为get post add delete
                data: '{}',//发送的参数
                url: 'http://localhost:33587/api/UserInfo', //请求的地址
                contentType: "application/json;charset=utf-8",//数据格式
                dataType: 'json',  //数据形式
                success: function (list) {
                    var tlist = $("#list");
                    tlist.empty();
                    $.each(list, function (index,item) {
                        tlist.append('<tr><td>' + item.ID + '</td><td>' + item.Name + '</td></tr>');
                    });
                }
            });
        }

    </script>
</head>
<body>
    <table border="1">
        <tr>
            <td>编号</td>
            <td>姓名</td>
        </tr>
        <tbody id="list"></tbody>
    </table>

</body>
</html>

2.HttpClient

 HttpClient client = new HttpClient();

            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            //执行Get操作
            HttpResponseMessage response = client.GetAsync("http://localhost:33587/api/UserInfo").Result;

           var lUsers=  response.Content.ReadAsAsync<List<UserInfo>>().Result;

           ViewData.Model = lUsers;

 

请求,由路由规则决定

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }

 

posted on 2016-09-20 14:55  浩凡儿  阅读(203)  评论(0编辑  收藏  举报