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

在Web Api中实现Http方法(Get,Put,Post,Delete)

时间:2017-06-15 23:33:10      阅读:421      评论:0      收藏:0      [点我收藏+]

标签:默认   str   .ajax   png   email   ret   readonly   技术分享   alt   

Post(新增),Put(修改),Delete(删除),Get(查询)

GET:生到数据列表(默认),或者得到一条实体数据

POST:添加服务端添加一条记录,记录实体为Form对象

PUT:添加或修改服务端的一条记录,记录实体的Form对象,记录主键以GET方式进行传输

DELETE:删除 服务端的一条记录

上面公开的API接口都是在XMLHttpRequest情况下调用的,当然你可以使用jquery的ajax组件来完成这个请求调用

<fieldset>
    <legend>测试Web Api
    </legend>
    <a href="javascript:add()">添加(post)</a>
    <a href="javascript:update(1)">更新(put)</a>
    <a href="javascript:deletes(1)">删除(delete)</a>
    <a href="/api/test">列表(Get)</a>
    <a href="/api/test/1">实体(Get)</a>
</fieldset>
<script>

    function add() {

        $.ajax({
            url    : "/api/Test/",
            type   : "POST",
            data   :{"UserID":4,"UserName":"test","UserEmail":"Parry@cnblogs.com"},
            success: function (data) { alert(JSON.stringify(data)); }
   
        });

    }

    //更新
    function update(id) {
        $.ajax({
            url    : "/api/Test?id="+id,
            type   : "Put",
            data   :{"UserID":1,"UserName":"moditest","UserEmail":"Parry@cnblogs.com"},
            success: function (data) { alert(JSON.stringify(data)); }
        });

    }
    function deletes(id) {
        $.ajax({
            url    : "/api/Test/1",
            type   : "DELETE",
            success: function (data) { alert(data);}
        });

    }
</script>
/// <summary>
    /// Test模块API
    /// URI:/Api/Test
    /// </summary>
    public class TestController : ApiController
    {

        /// <summary>
        /// User Data List
        /// </summary>
        private readonly List<Users> _userList = new List<Users>
       {
           new Users {UserID = 1, UserName = "zzl", UserEmail = "bfyxzls@sina.com"},
           new Users {UserID = 2, UserName = "Spiderman", UserEmail = "Spiderman@cnblogs.com"},
           new Users {UserID = 3, UserName = "Batman", UserEmail = "Batman@cnblogs.com"}
       };
        /// <summary>
        /// 得到列表对象
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Users> Get()
        {
            return _userList;
        }

        /// <summary>
        /// 得到一个实体,根据主键
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Users Get(int id)
        {
            return _userList.FirstOrDefault(i => i.UserID == id);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="form">表单对象,它是唯一的</param>
        /// <returns></returns>
        public Users Post([FromBody] Users entity)
        {
            _userList.Add(entity);
            return entity;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="form">表单对象,它是唯一的</param>
        /// <returns></returns>
        public Users Put(int id, [FromBody]Users entity)
        {
            var user = _userList.FirstOrDefault(i => i.UserID == id);
            if (user != null)
            {
                user.UserName = entity.UserName;
                user.UserEmail = entity.UserEmail;
            }
            return user;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public void Delete(int id)
        {
         _userList.Remove(_userList.FirstOrDefault(i=>i.UserID==id));
        }
    }

技术分享技术分享

 

在Web Api中实现Http方法(Get,Put,Post,Delete)

标签:默认   str   .ajax   png   email   ret   readonly   技术分享   alt   

原文地址:http://www.cnblogs.com/914556495wxkj/p/7020522.html

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