WEBAPI获取数据

在大学学期期间学习的从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>

 

posted @ 2019-05-29 17:33  Saunterer  阅读(2091)  评论(1编辑  收藏  举报