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

WebAPI

时间:2016-09-20 15:11:39      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:


====》两种网络服务
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 }
            );
        }
    }

 

WebAPI

标签:

原文地址:http://www.cnblogs.com/haofaner/p/5888851.html

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