当前位置:编程学习 > ASP.NET> 正文

webapi 参数的传递

时间:2016-6-13类别:编程学习

webapi 参数的传递

webapi 参数的传递

一、Get传递方式

 

1、基础类型参数

 

  •  
  • JScript 代码   复制
  • 
    $.ajax({
            type: "get",
            url: "localhost:80/api/Charging/GetAllChargingData",
            data: { id: 1, name: "Jim", bir: "1988-09-11"},
            success: function (data, status) {
                if (status == "success") {
                    $("#li_test").html(data);
                }
            }
        });
    
    		
  •  
  •  
  •  
  • C# 代码   复制
  • 
    [HttpGet]
    public string GetAllChargingData(int id, string name)
    {
        return "ChargingData" + id;
    }
    
    				
  •  

    2、实体作为参数

    可以给方法的参数加上[FromUri],即可直接接收实体参数

     

  •  
  • JScript 代码   复制
  • 
    $.ajax({
            type: "get",
            url: "localhost:80/api/Charging/GetByModel",
            contentType: "application/json",
            data: { ID: "1", NAME: "Jim", CREATETIME: "1988-09-11" },
            success: function (data, status) {
                if (status == "success") {
                    $("#li_test").html(data);
                }
            }
        });
    
    						
  •  

  • C# 代码   复制
  • 
        public class TB_CHARGING
        {
            /// 
            /// 主键Id
            /// 
            public string ID { get; set; }
    
            /// 
            /// 充电设备名称
            /// 
            public string NAME { get; set; }
    
            /// 
            /// 充电设备描述
            /// 
            public string DES { get; set; }
    
            /// 
            /// 创建时间
            /// 
            public DateTime CREATETIME { get; set; }
        }
    
      [HttpGet]
      public string GetAllChargingData([FromUri]TB_CHARGING obj)
      {
            return "ChargingData" + obj.ID;
      }
    
    						
  •  

    二、Post 传递方式

     

    1、单个基础类型参数

     

  • JScript 代码   复制
  • 
    $.ajax({
            type: "post",
            url: "localhost:80/api/Charging/SaveData",
            data: { "": "Jim" },
            success: function (data, status) {}
        });
    
    		
  •  
  •  
  •  
  • C# 代码   复制
  • 
    
    [HttpPost]
    public bool SaveData([FromBody]string NAME)
    {
        return true;
    }
    
    				
  •  

    2、多个基础类型参数

  •  
  •  
  • JScript 代码   复制
  • 
     $.ajax({
            type:"post",
            url:"localhost:80/api/Charging/SaveData",
            contentType:'application/json',
            data:JSON.stringify({ NAME: "Jim",DES:"备注" }),
            success:function (data, status){}
        });
    
    				
  •  

  • C# 代码   复制
  • 
       [HttpPost]
        public object SaveData(dynamic obj)
        {
            var strName = Convert.ToString(obj.NAME);
            return strName;
        }
    
    
    				
  • 
    
    

     

    3、单个实体作为参数

     

  • JScript 代码   复制
  • 
     $.ajax({
            type: "post",
            url: "localhost:80/api/Charging/SaveData",
            data: { ID: "1", NAME: "Jim", CREATETIME: "1988-09-11" },
            success: function (data, status) {}
        });
    
    		
  •  
  •  
  •  
  • C# 代码   复制
  • 
       [HttpPost]
       public bool SaveData(TB_CHARGING oData)
       {
           return true;
       }
    
    		
  •  

    4、实体和基础类型一起作为参数传递

  •  
  •  
  • C# 代码   复制
  • 
    var postdata = { ID: "1", NAME: "Jim", CREATETIME: "1988-09-11" };
        $.ajax({
            type: "post",
            url: "localhost:80/api/Charging/SaveData",
            contentType: 'application/json标签:
  • 上一篇下一篇

    猜您喜欢

    热门推荐