在大学学期期间学习的从mvc中的webapi中取数据

直接看代码

首先是控制器中的

 

using system;
using system.collections.generic;
using system.linq;
using system.net;
using system.net.http;
using system.web;
using system.web.http;
using system.web.security;


namespace webapi.controllers
{
    //简历一个类存储数据
    public class users
    {
        public int userid { get; set; }
        public string username { get; set; }
        public string pwd { get; set; }
    }
    //存储数据
    public class usercontroller : apicontroller
    {
        private list<users> _userlist = new list<users>()
       {
           new users{userid=1,username="jake1",pwd="123"},
           new users{userid=2,username="jake2",pwd="123"},
           new users{userid=3,username="jake3",pwd="123"},
       };

        //get获取数据

        //获取所有数据
        //api/user/get
        public ienumerable<users> get()
        {
            
            return _userlist;
        }
        //根据id获取当数据
        public users get(int id)
        {
            dynamic u;
            u = (from c in _userlist where c.userid.equals(id) select c).firstordefault();//取到第一条数据
            u = _userlist.firstordefault(p => p.userid.equals(id));
            return u;

        }
        //根据对象获取数据
        public users getmodel([fromuri]users u)
        {
            dynamic us;
           /* u = (from c in _userlist where c.userid.equals(id) select c).firstordefault();*///取到第一条数据
            us = _userlist.firstordefault(p => p.userid.equals(u.userid)&&p.username.equals(u.username));
            return us;

        }

        //post获取数据

           //获取所有数据
        [httppost]
        public ienumerable<users> getuser()
        {

            return _userlist;
        }
        //根据id获取单个数据
        [httppost]
        public ienumerable<users> getuser1([frombody]int id)
        {

            return _userlist;
        }
        
    }
}

 

 

视图中的

 

@{
    layout = null;
}

<!doctype html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>index</title>
    <script src="~/scripts/jquery-3.3.1.js"></script>
</head>
<body>
    get部分
    <div>
        <input type="button" id="btn1" value="返回所有数据" />
        <br />
        <input type="button" id="btn2" value="返回指定数据" />
        <br />
        <input type="button" id="btn3" value="封装用户数据传递" />
        <br />

    </div>


    post部分
    <div>
        <input type="button" id="btn4" value="返回所有数据" />
        <br />
        <input type="button" id="btn5" value="返回指定数据" />
        <br />
        <input type="button" id="btn6" value="封装用户数据传递" />
        <br />
        用户名:<input type="text" id="txtname" name="txtname" />
        密码:<input type="text" id="txtpwd" name="txtpwd" />
        <input type="button" value="登录" />
    </div>
    <script>
        $(function () {
            $("#btn1").click(function () {
                $.ajax({
                    type: 'get',
                    url: '/api/user/get',
                    datatype: 'json',
                    success: function (res) {
                        alert(res);
                    }
                })
            })


            $("#btn2").click(function () {
                $.ajax({
                    type: 'get',
                    url: '/api/user/get/1',
                    datatype: 'json',
                    success: function (res) {
                        alert(res);
                    }
                })
            })


            $("#btn3").click(function () {
                var user = { userid: 1, username:'jake', pwd: '123' };
                $.ajax({
                    type: 'get',
                    url: '/api/user/getmodel/',
                    datatype: 'json',
                    data: user,
                    success: function (res) {
                        alert(res);
                    }
                })
            })

            $("#btn4").click(function () {
               
                $.ajax({
                    type: 'post',
                    url: '/api/user/getuser/',
                    datatype: 'json',
                    
                    success: function (res) {
                        alert(res);
                    }
                })
            })


            $("#btn5").click(function () {
                var d = { "": "1" };
                $.ajax({
                    type: 'post',
                    url: '/api/user/getuser1/',
                    datatype: 'json',
                    data:d,
                    success: function (res) {
                        alert(res);
                    }
                })
            })

           
            })
        })
    </script>
</body>
</html>