webapi 参数的传递
类别:编程学习 浏览量:1264
时间:2016-6-13 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
标签:webapi
您可能感兴趣
- webapi 参数的传递
- 使用Visual Studio为WebAPI生成帮助文档
- javascript dom事件模型(JavaScript WebAPI、DOM、事件和操作元素实例详解)
- 使用Fiddler测试WebApi接口
- 阴生环境 耐阴地被植物,你知道哪些(阴生环境耐阴地被植物)
- 常见的喜阴植物有哪些 养室内盆栽就在这里选(常见的喜阴植物有哪些)
- 这8种耐阴植物,营造阴生植物花境,也是一个不错的选择(营造阴生植物花境)
- 览邦G08 Plus SMART WATCH 测评⑱ 全独立这才是智能手表该有的样子(览邦G08PlusSMART)
- 荣耀手表 GS 3 真机亮相 不支持无线充电(荣耀手表GS3)
- 通过体温就能为智能手表充电 原来是用NASA在空间站用的黑科技(通过体温就能为智能手表充电)
热门推荐
- sqlserver的图形表(SQL Server纵表与横表相互转换的方法)
- mysql安装失败原因和解决方法(MySQL MGR搭建过程中常遇见的问题及解决办法)
- canvas两种形式动画(用canvas做一个DVD待机动画的实现代码)
- 网站提高服务器响应(网站解决和优化Server is too busy的一些方法)
- linux时间同步ubuntu(在Ubuntu中使用NTP实现时间同步)
- python如何将xls转成xlsx(Python这样操作能存储100多万行的xlsx文件)
- 云服务器哪个安全速度快(云服务器速度快不快?有哪些测试办法?)
- php处理app上传的图片(php用wangeditor3实现图片上传功能)
- 软件测试常用mysql语句(MySQL压力测试工具Mysqlslap的使用)
- LINQ中Aggregate的用法