zoukankan      html  css  js  c++  java
  • 在Web Api中实现Http方法(Get,Put,Post,Delete)

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

  • 相关阅读:
    25 自动类型转换
    24 枚举Enum类
    23 Collection集合常用方法讲解
    Eclipse 快捷键
    21 泛型
    20 IO流(十七)——Commons工具包,FileUtils(二)——copy方法
    19 IO流(十六)——Commons工具包,FileUtils(一)
    18 IO流(十五)——RandomAccessFile随机访问文件及使用它进行大文件切割的方法
    CentOS6.5-6.9安装 docker
    linux开启端口
  • 原文地址:https://www.cnblogs.com/914556495wxkj/p/7020522.html
Copyright © 2011-2022 走看看