MVC中的webApi

来源:互联网 发布:云南管家婆软件总代理 编辑:程序博客网 时间:2024/05/29 13:19
namespace t3_WebAPI.Controllers//基于资源的{    public class UserInfoController : ApiController//继承apicontroller    {        // GET api/userinfo        //使用Method=Get的方式请求url=api/userinfo,则这个方法会被调用执行        //查询所有信息        public IEnumerable<UserInfo> Get()        {            List<UserInfo> list=new List<UserInfo>();            list.Add(new UserInfo()            {                Id = 1,                Name = "clx"            });            list.Add(new UserInfo()            {                Id = 2,                Name = "gj"            });            list.Add(new UserInfo()            {                Id = 3,                Name = "hr"            });            list.Add(new UserInfo()            {                Id = 4,                Name = "hqg"            });            return list;        }        // GET api/userinfo/5        //查询单条信息        public string Get(int id)        {            return "value";        }        // POST api/userinfo        //增加        //public void Post(string value)        //{        //}        [HttpPost]        public void Add(string value)        {                    }        // PUT api/userinfo/5        //修改        [HttpPut]        public void Put(int id, string value)        {        }        // DELETE api/userinfo/5        //删除        [HttpDelete]        public void Delete(int id)        {        }    }}
1.上面的方法的访问路径:
localhost:8080/api/uerinfo(默认html形式展示)

2.怎么调用(两种方式的区别就是第二种可以跨域)

方式一:jquery的ajax

指定请求的数据类型: contentType: "application/json; charset=utf-8",//数据类型
主要的属性:
type:请求方式,包括Get、Post、Put、Delete
url:请求资源,根据路由规则编写
data:请求数据,为json格式
contentType:请求数据的类型及编码
dataType:返回的数据类型,可以是text、json
success:成功处理的回调函数
备注中为修改请求的示例
注意:使用js的异步操作,不可以跨域访问

<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <title></title>    <script src="Scripts/jquery-1.7.1.min.js"></script>    <script>        $(function() {            LoadList();        });        function LoadList() {            $.ajax({                type: 'get',//请求方式,可以为Get,Post,Put,Delete                data: '{}',//发送的参数                url: 'http://localhost:2460/api/userinfo',//请求地址                contentType: "application/json; charset=utf-8",//数据类型(必须加)                dataType: 'json',                success: function(list) {                    var list1 = $('#list');                    list1.empty();                    $.each(list, function(index, item) {                        list1.append('<tr><td>' + item.Id + '</td><td>' + item.Name + '</td></tr>');                    });                }            });        }    </script></head><body>        <table border="1">        <tr>            <th>编号</th>            <th>姓名</th>        </tr>        <tbody id="list">                    </tbody>    </table></body>

方式二:HttpClient对象

namespace t3_Client.Controllers{    public class HomeController : Controller    {        //        // GET: /Home/        public ActionResult Index()        {            //客户端对象的创建与初始化            HttpClient client=new HttpClient();            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));            //执行Get操作            HttpResponseMessage response= client.GetAsync("http://localhost:2460/api/userinfo").Result;            var list= response.Content.ReadAsAsync<List<UserInfo>>().Result;//异步的(读取正文数据ReadAsAsync用泛型可以指定类型)            ViewData.Model = list;            return View();        }    }}

两种web服务
SOAP风格:基于方法,产品是WebService
REST风格:基于资源,产品是WebAPI
可以返回json、xml类型的数据


0 0