webApi中参数传递

webApi中参数传递

一:无参数的get方法;

前端:   

 function GetNoParam() {
        //为了统一:我们都采用$.ajax({}) 方法;
        $.ajax({
            url: '/api/User/GetNoParam',
            type: 'get',
            dataType: 'application/json',  //application/xml webapi 返回的数据类型,客户端请求时添加在请求头中
                                          //当无法转换成客户想要的数据时,将报错;
            success: function (data) {
                alert(data);
                $.each(data, function (index, item) {
                    alert(item["Name"]);
                })
            }
        });
    }

后端:

   public List<User> GetNoParam()
        {
            var userList = new List<User>
            {
                new User{Id=1,Name="Jack"},
                new User{Id=2,Name="Frank"}
            };

            return userList;

        }

二:get+一个参数

前端:

 //通过get传递一个参数的方法;
    function GetByOneParam() {
        $.ajax({
            url: '/api/User/GetByOneParam',
            type: 'get',
            data:{"Name":"Jack"},
            dataType: 'json',
            success: function (data) {
                var value = data;
            }
        });
    }

后端:

  public List<User> GetByOneParam(string Name)
        {
            //这里我们传递一个参数:
            var userList = new List<User>
            {
                new User{Id=1,Name="Jack"},
                new User{Id=2,Name="Frank"}
            };
            var query = (from u in userList
                          where u.Name == Name
                          select u
                         ).ToList();
            return query;
        }

三:get+两个参数或以上;

前端:

function GetByTwoParam() {
        $.ajax({
            url: '/api/User/GetByOneParam',
            type: 'get',
            data:{"Name":"Jack","Id":1},
            dataType: 'json',
            success: function (data) {
                var value = data;
            }
        });
    }

后端:

   public List<User> GetByTwoParam(string Name,int Id)
        {
            //这里我们传递一个参数:
            var userList = new List<User>
            {
                new User{Id=1,Name="Jack"},
                new User{Id=2,Name="Frank"}
            };
            var query = (from u in userList
                          where u.Name == Name && u.Id==Id
                          select u
                         ).ToList();
            return query;
        }

四:POST无参数返回数据值

 前端:

    //通过post的方法来获取数据;
    function PostNoParam() {
        $.ajax({
            url: '/api/User/PostNoParam',
            type: 'Post',//post方式
            dataType: 'json',
            success: function (data) {
                var value = data;
            }
        });
    }

 后端:

 public List<User> PostNoParam()
        {
            var userList = new List<User>
            {
                new User{Id=1,Name="Jack"},
                new User{Id=2,Name="Frank"}
            };

            return userList;
        }

五:post+一个参数(小心这里有陷阱)

前端:

 function PostByOneParam() {
        $.ajax({
            url: '/api/User/PostByOneParam',
            type: 'Post',//post方式
            data:{"":"Jack"}, //注意这里的key值是空的
            dataType: 'json',
            success: function (data) {
                var value = data;
            }
        });

    }

后端:

      //注意这里的关键是添加一个:FromBody
      // to force web api to read simple type from the request body
      // add [FromBody] attribute to the para
      //[FromBody] 就告诉Web API 要从post请求体重去获取参数的值。
      //Web API 的模型绑定器希望找到 [FromBody] 并不是我们常见的 key=value 的键名的值,也就是说, 不是 key=value ,而是 =value 。
      //在前端key为空值
      //还有一个特点:[FromBody] 修饰的参数只能有一个。我们需要对传递的多个参数进行封装才行。
        public List<User> PostByOneParam([FromBody]string Name)
        {
            var userList = new List<User>
            {
                new User{Id=1,Name="Jack"},
                new User{Id=2,Name="Frank"}
            };
            var query = ( from u in userList
                          where u.Name == Name
                          select u
                         ).ToList();


            return query;
        }

六:POST 提交多个参数;

 前端:

 //组合多个参数
    function PostByMoreParam() {
        //或者这种方式也可以的额
        var data = { Name: "jack", Id: 1 };
        $.ajax({
            url: '/api/User/PostByMoreParam',
            type: 'Post',//post方式
            data: data,  //这样传递对象 后台是能够获取到值滴呀
            dataType: 'json',
            success: function (data) {
                var value = data;
            }
        });
    }
   //或则这样写
    function PostByMoreParam1() {
        var data = { Name: "jack", Id: 1 };
        var value = JSON.stringify(data);
        //使用JSON.stringify转化一下;
        //传递的数据格式是json字符串
        $.ajax({
            url: '/api/User/PostByMoreParam',
            type: 'Post',//post方式
            data: value, 
            dataType: 'json',
            contentType: 'application/json', //如果你使用了 JSON.stringify将对象转化成了字符串,那么这里就要添加这个;
            success: function (data) {
                var value = data;
            }
        });
        //application/x-www-form-urlencoded 表示的是:窗体数据被编码为名称/值对。
        //并不是json格式对于的 application/json
    }

后端:

 public List<User> PostByMoreParam(User user)  //这里是实体对象滴呀
        {
            var userList = new List<User>
            {
                new User{Id=1,Name="Jack"},
                new User{Id=2,Name="Frank"}
            };
            var query = (from u in userList
                         where u.Name == user.Name && u.Id == user.Id
                         select u
                         ).ToList();


            return query;
        }

7.传递多个不同对象的Post请求

前台:

function PostMoreObj() {
        var stu = { Name: "jack", Id: 1 };
        var course = { Id: 1, CourseName: "语文", SId: 1 };
        //尝试直接这样传递看看得得行滴呀;
        $.ajax({
            url: '/api/User/PostMoreObj',
            type: 'Post',//post方式
            data: { "user": stu, "course": course },
            dataType: 'json',
            success: function (data) {
                alert(typeof(data));
                var value = data;
            }
        });
    }

后台:

 //有时候我们还会遇到需要传递多个不同对象参数的情况,
        //对于这种特殊的情况在 Json.Net 中为我们提供了一个 名为 JObject 的通用对象容器
        public bool PostMoreObj(Newtonsoft.Json.Linq.JObject jData)
        {
            dynamic json = jData;
            Newtonsoft.Json.Linq.JObject juser = json.user;
            Newtonsoft.Json.Linq.JObject jcourse = json.course;

            var stu   =  juser.ToObject<User>();
            var course = jcourse.ToObject<CourseInfo>(); //然后就转化成了我们想要的数据滴呀;

            return true;
        }

 不过在新的公司里面,采用的是代理的方式,所以这些方法可能暂时用不到(是不是觉得很高端,代理哦~)

原文地址:https://www.cnblogs.com/mc67/p/5698407.html