vue.js - axios Get、Post方法传参给 .net core webapi。

一:axios中是 Get请求:

1:在vue项目中通过params属性携带数据:

    let _self = this;
          axios({
            method:'get',
            url:'http://localhost:5000/api/StuInFors/GetEFAsync/',
            params:{pagesize:10,pageindex:2}
          }).then(function(resp){
            //document.write(JSON.stringify(resp.data));
            console.log(resp.status);
            _self.apidatas = resp.data;

          });


2:然后.net core webapi 中通过Query取出数据:

  [HttpGet]
        public async Task<List<InforEF>> GetEFAsync()//int pagesize,int pageindex)
        {
            int pagesize = 10;//页大小。
            int pageindex = 1;//第几页。
            if (Request.Query.ContainsKey("pagesize"))
            {
                pagesize = Convert.ToInt32(Request.Query["pagesize"]);
                pageindex = Convert.ToInt32(Request.Query["pageindex"]);
            }


            //Skip(5),忽略前面的 5 个
            //int pagesize = 10;//页大小。
            //int pageindex = 1;//第几页。
            //所以Skip(pagesize * (pageindex - 1)),Take(pagesize);
            List<InforEF> infors = await _context.Infortb.OrderBy(infor => infor.Fid).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToListAsync();
            return infors;
        }

  二:axios中是 Post 请求:

  1:vue中通过params或者data属性携带数据:

  rowupdateclick(row) //added by longdb 2019.2.16
     {
         let _self = this;

        this.$confirm('保存, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
        
            axios({
            method:'post',
            data:[{
              'FSaleNo':row.FSaleNo,
              'FSaleName':row.FSaleName ,
              'FSaleQty':row.FSaleQty,
              'FSaleAmount':row.FSaleAmount,
              'FSaleType':row.FSaleType,
              'FSaleStatus':row.FSaleStatus
            }],
            params:{FSaleNo:row.FSaleNo,FSaleAmount:row.FSaleAmount},
            url:"http://localhost:5000/api/SalesOrder/PostForUpdate/",//'http://172.17.192.161/WebApiAndroid/api/StuInFors/Post/',
          }).then(function(resp){
            console.log(resp.status);
           if(resp.status == 200)
           {
              _self.$message({
            type: 'success',
            message: '保存成功!'+resp.data
          });
           }
          });
         
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消保存'
          });
        });
     }

2:如果是通过params属性,则在.net core webapi中的取法同Get请求。如果是通过data属性,则如下取法:

  public async Task<string> PostForUpdate()//[FromBody] string value)
        {
            string retvalue = "failed";
            Stream stream = Request.Body;
            
            if (stream != null)
            {
                using (var reader = new StreamReader(stream, Encoding.UTF8))
                {
                    string data = await reader.ReadToEndAsync();

                    DataTable dt = JSONInterFace.JsonToDatableWithJsonNet(data);

                    if (dt != null && dt.Rows.Count > 0)
                    {
                        string sql = "update TSaleOrder set FSaleAmount='" + dt.Rows[0]["FSaleAmount"].ToString() + "' where FSaleNo='" + dt.Rows[0]["FSaleNo"].ToString() + "'";
                        if (DBHelper.DBExecute(sql))
                        {
                            retvalue = "success";
                        }
                        //retvalue = DBHelper.sqlBulkCopyData(dt, "TSaleOrder");
                    }
                }
            }
            return retvalue;
        }

 

posted @ 2019-02-15 16:51  longdb  阅读(4662)  评论(0编辑  收藏  举报