WEBAPI get data

Mvc fetch data from the webapi during the university semester

Direct look at the code

The first is the controller

 

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= {The Userid the Users . 3 , the Username = " jake3 " , pwd = " 123 " }, 
       }; 

        // GET acquiring data 

        // get all data
         // API / the User / the Get 
        public the IEnumerable <the Users> the Get () 
        { 
            
            return _userlist; 
        } 
        // ID when the acquired data in accordance with 
        public the Users the Get ( int ID) 
        { 
            Dynamic U; 
            U = ( from C in _userlist WHERE c.Userid.Equals (ID) SELECTC) .FirstOrDefault (); // get the data to the first 
            U = _userlist.FirstOrDefault (P => p.Userid.Equals (ID));
             return U; 

        } 
        // get the data according to the object 
        public the Users getModel ([FromUri ] the Users U) 
        { 
            Dynamic US;
            / * U = (C in _userlist from WHERE c.Userid.Equals (ID) SELECT C) .FirstOrDefault (); * /// taken to the first data 
            us = _userlist.FirstOrDefault ( = P> p.Userid.Equals (u.Userid) && p.Username.Equals (u.Username));
             return US; 

        } 

        // POST data acquisition 

           // get all data
        [HttpPost]
         public the IEnumerable <the Users> the GetUser () 
        { 

            return _userlist; 
        } 
        // get a single data according to ID 
        [HttpPost]
         public the IEnumerable <the Users> GetUser1 ([FromBody] int ID) 
        { 

            return _userlist; 
        } 
        
    } 
}

 

 

View

 

@{
    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=""
        <INPUT type =/>"Returns all the data
        <br /> Button " ID = " btn2 " value = " returns the specified data " /> 
        <br /> 
        <INPUT type = " Button " ID = " btn3 " value = " encapsulated user data transfer " /> 
        <br /> 

    </ div> 


    POST portion
     <div> 
        <INPUT type = " Button " ID = " btn4 " value = " returns all the data " />
        <br />
        <input type="button" ID = " btn5 " value = " returns the specified data " /> 
        <br /> 
        <INPUT type = " Button " ID = " btn6 " value = " encapsulated user data transfer " /> 
        <br /> 
        Username: <INPUT = type " text " ID = " txtName " name = " txtName " /> 
        password: <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>

 

Guess you like

Origin www.cnblogs.com/sjrcwy/p/10945162.html