webapi 参数的传递
webapi 参数的传递一、Get传递方式
1、基础类型参数
$.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);
}
}
});
[HttpGet]
public string GetAllChargingData(int id, string name)
{
return "ChargingData" + id;
}
2、实体作为参数
可以给方法的参数加上[FromUri],即可直接接收实体参数
$.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);
}
}
});
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、单个基础类型参数
$.ajax({
type: "post",
url: "localhost:80/api/Charging/SaveData",
data: { "": "Jim" },
success: function (data, status) {}
});
[HttpPost]
public bool SaveData([FromBody]string NAME)
{
return true;
}
2、多个基础类型参数
$.ajax({
type:"post",
url:"localhost:80/api/Charging/SaveData",
contentType:'application/json',
data:JSON.stringify({ NAME: "Jim",DES:"备注" }),
success:function (data, status){}
});
[HttpPost]
public object SaveData(dynamic obj)
{
var strName = Convert.ToString(obj.NAME);
return strName;
}
3、单个实体作为参数
$.ajax({
type: "post",
url: "localhost:80/api/Charging/SaveData",
data: { ID: "1", NAME: "Jim", CREATETIME: "1988-09-11" },
success: function (data, status) {}
});
[HttpPost]
public bool SaveData(TB_CHARGING oData)
{
return true;
}
4、实体和基础类型一起作为参数传递
var postdata = { ID: "1", NAME: "Jim", CREATETIME: "1988-09-11" };
$.ajax({
type: "post",
url: "localhost:80/api/Charging/SaveData",
contentType: 'application/json标签: