Web API 接口参数解析
- get请求(数据都会包括在url地址上)
- 基础类型参数的get请求
1 //web api示例 2 [HttpGet] 3 public string GetAllChargingData(int id,string name){ 4 return "Charging" + id + " " + name; 5 }
1 //AngularJS请求 2 $http.get("/GetAllChargingData?id=1&name=Jim").then(res => { }, () => { }) //Charging1 Jim 3 $http.get("/GetAllChargingData", {params: {id: 1,name: "aaaa"}}).then(res => { }, () => {})//Charging1 aaaa 4 //对于ajax 5 $.ajax({ 6 type: "get", 7 url: "/GetAllChargingData", 8 data: { id: 1,name: "Jim",bir: "1988-09-11" }, 9 success: function (data, status) { if (status == "success") { } }// Charging1 Jim 10 });
- 实体作为参数的get请求
- 对于第一个接口,直接将json对象当做实体传递给后台,后台无法接受到,所以AngularJS请求报错415,Ajax请求则是接受的null
- 对于第二个接口,在接口的参数加上[FromUri]特性(强制Web API从URL读取复杂类型),这样AngularJS请求和Ajax请求都可以接受到数据
- 对于第三个接口,这是在页面请求上使用JSON.stringify()方法对json数据进行序列化,然后在接口中反序列化,URL地址上后面会加上一串编码
1 [HttpGet]//TB_CHARGING 是一个类 2 public JsonResult<TB_CHARGING> GetByModel(TB_CHARGING oData){ 3 return Json(oData); 4 } 5 [HttpGet] 6 public JsonResult<TB_CHARGING> GetByModelFromUri([FromUri]TB_CHARGING oData){ 7 return Json(oData); 8 } 9 [HttpGet] 10 public JsonResult<TB_CHARGING> GetByModelstring(string strQuery){ 11 TB_CHARGING oData = Newtonsoft.Json.JsonConvert.DeserializeObject<TB_CHARGING>(strQuery);//反序列化 12 return Json(oData); 13 }
- 数组作为参数
- 在Web API中,方法名以Get开头,Web API会自动默认为get请求,但是如果使用其他名称开头但是又不使用特性标记,可能在请求时报错405,所以所有的Web API接口都应该加上([HttpGet]/[HttpPost]/[HttpPut]/[HttpDelete])的特性
- post请求(参数放在请求体上)
- 基础类型参数
- 第一种接口,Ajax请求时无法传入参数,但是AngularJS可以请求成功
- 第二个接口加上[FromBody]特性(可以强制Web API从请求正文中读取简单类型),且([FromBody]string NAME,[FromBody]string DES)这样是错误使用,应该使用第三种接口使用dynamic类型,对于第二种接口,AngularJS请求时无法传入参数,但是Ajax可以请求成功
1 [HttpPost] 2 public string SaveData_ng(string NAME){ 3 return NAME; 4 } 5 [HttpPost] 6 public string SaveData([FromBody]string NAME){ 7 return NAME; 8 } 9 [HttpPost]//推荐写法 10 public object SaveData_obj(dynamic obj){ 11 var strName = Convert.ToString(obj.NAME); 12 return strName; 13 }
1 //AngularJS请求 2 $http.post("/SaveData_ng?NAME=jam", {}, {}).then(res => {}, () => {});//jam 3 $http.post("/SaveData_obj", { NAME: "Jim", DES: "备注" }, {}).then(res => {}, () => {});//Jim 4 //Ajax请求 5 $.ajax({ 6 type: "post", 7 url: "/SaveData", 8 data: { "": "JIM" }, 9 success: function (data, status) { if (status == "success") {} }//"JIM" 10 }) 11 $.ajax({ 12 type: "post", 13 url: "/SaveData_obj", 14 contentType: 'application/json',//需要加上 15 data: JSON.stringify({ NAME: "Jim", DES: "备注" }), 16 success: function (data, status) { if (status == "success") {} }//"Jim" 17 })
- 实体类型参数
1 [HttpPost] 2 public TB_CHARGING SaveData_oData(TB_CHARGING oData){ 3 return oData; 4 }
1 var postdata = { ID: "1", NAME: "Jim", CREATETIME: "1988-09-11" }; 2 //AngularJS请求 3 $http.post("/SaveData_oData", postdata, {}).then(res => { }, () => {});//返回实体参数 4 //Ajax请求 5 $.ajax({ 6 type: "post", 7 url: "/SaveData_oData", 8 data: postdata,//该写法的contentType默认为application/x-www-form-urlencoded 目前写法 9 //还有一种写法 contentType: "application/json", data: JSON.stringify(postdata), 10 success: function (data, status) { if (status == "success") {} }//返回实体参数 11 })
- 实体类型和基础类型一起作为参数
1 [HttpPost] 2 public object SaveData_objs(dynamic obj){ 3 var strName = Convert.ToString(obj.NAME); 4 var oCharging = Newtonsoft.Json.JsonConvert.DeserializeObject<TB_CHARGING>(Convert.ToString(obj.Charging)); 5 return strName; 6 }
1 //AngularJS请求 2 $http.post("/SaveData_objs", { NAME: "Lilei", Charging: postdata }, {}).then(res => { }, () => {});//Lilei 3 //Ajax请求 4 $.ajax({ 5 type: "post", 6 url: "/SaveData_objs", 7 contentType: 'application/json', 8 data: JSON.stringify({ NAME: "Lilei", Charging: postdata }), 9 success: function (data, status) { if (status == "success") {} }//Lilei 10 });
- 数组类型参数(基础类型的数组,实体集合)
1 [HttpPost] 2 public string[] SaveData_Array(string[] ids){ 3 return ids; 4 } 5 [HttpPost] 6 public List<TB_CHARGING> SaveData_List(List<TB_CHARGING> lstCharging){ 7 return lstCharging; 8 }
1 var arr = ["1", "2", "3", "4"]; 2 var list = [postdata,postdata,postdata] 3 //AngularJS请求 4 $http.post("/SaveData_Array", arr, {}).then(res => { }, () => {});//["1","2","3","4"] 5 $http.post("/SaveData_List", list, {}).then(res => { }, () => {});//list集合 6 //Ajax请求 7 $.ajax({ 8 type: "post", 9 url: "/SaveData_Array", 10 contentType: 'application/json', 11 data: JSON.stringify(arr), 12 success: function (data, status) { if (status == "success") {} }//["1","2","3","4"] 13 }); 14 $.ajax({ 15 type: "post", 16 url: "/SaveData_List", 17 contentType: 'application/json', 18 data: JSON.stringify(list), 19 success: function (data, status) { if (status == "success") {} }//list集合 20 });
- put请求和delete请求与post请求相似
1 public class TB_CHARGING{ 2 public string ID { get; set; } 3 public string NAME { get; set; } 4 public string DES { get; set; } 5 public DateTime CREATETIME { get; set; } 6 }