ASP.NET MVC——Web Api
GET方式
[HttpGet] public IEnumerable<Person> GetList() { return list; } [HttpGet] public IEnumerable<Person> GetOne(int id) { var p = list.Where<Person>(r => r.Id == id); return p; } [HttpGet] public IEnumerable<Person> GetOne(string name) { var p = list.Where<Person>(r => r.Name == name); return p; } [HttpGet] public IEnumerable<Person> getList(string name, bool sex) { var p = list.Where<Person>(r => r.Name == name && r.Sex == sex); return p; }
调用
对应第一种get
$.ajax({ url: "http://localhost:14788/api/test", type: "get", dataType: "json", success: function (data) { var html = ''; $.each(data, function (index, value) { html += "<tr><td>" + value.Id + "</td><td>" + value.Name + "</td><td>" + value.Sex + "</td><td>" + value.Age + "</td></tr>"; }); $("#t").append(html); } });

由于默认的路由关系,对应第二种get方式
$.ajax({ url: "http://localhost:14788/api/test/1", type: "get", dataType: "json", success: function (data) { var html = ''; $.each(data, function (index, value) { html += "<tr><td>" + value.Id + "</td><td>" + value.Name + "</td><td>" + value.Sex + "</td><td>" + value.Age + "</td></tr>"; }); $("#t").append(html); } });

注意: 凡是走这种路由的方式 api/controller/id 这种形式的时候,无论有多少个只有一个参数的get方法,他只走一个,那就是参数是id 的,如果没有参数是id的方法,那他就走 api/controller方法,如果走的是 api/controller?a=qwe&b=asd这种形式的话,那就不走id着了,而是针对不同参数走不同参数的方法,参数除了不区分大小写外,还要保持一致。
通过?a=b方式,对应第3种get方式(可以通过更改路由规则,就像第2种似的)
$.ajax({ url: "http://localhost:14788/api/test", type: "get", dataType: "json", data:{name:"001"}, success: function (data) { var html = ''; $.each(data, function (index, value) { html += "<tr><td>" + value.Id + "</td><td>" + value.Name + "</td><td>" + value.Sex + "</td><td>" + value.Age + "</td></tr>"; }); $("#t").append(html); } });

同第3种方式,对应第4个get方式
$.ajax({ url: "http://localhost:14788/api/test", type: "get", dataType: "json", data: { name: "001", sex: true }, success: function (data) { var html = ''; $.each(data, function (index, value) { html += "<tr><td>" + value.Id + "</td><td>" + value.Name + "</td><td>" + value.Sex + "</td><td>" + value.Age + "</td></tr>"; }); $("#t").append(html); } });

==============================================================================================================
POST方式:
1、传递model
[HttpPost] public IEnumerable<Person> Add(Person p) { list.Add(p); return list; }
调用:
$.ajax({ url: "http://localhost:14788/api/test", type: "post", dataType: "json", data: { id: 6, name: "006", sex: true, age: 25 }, success: function (data) { var html = ''; $.each(data, function (index, value) { html += "<tr><td>" + value.Id + "</td><td>" + value.Name + "</td><td>" + value.Sex + "</td><td>" + value.Age + "</td></tr>"; }); $("#t").append(html); } });

可以看到,此时是以post表单方式以键值对的形式传递给api接口,自动转换成我们需要的model形式。
如果以json形式发送到接口呢?
$.ajax({ url: "http://localhost:14788/api/test", type: "post", dataType: "json", contentType: "application/json", data: { id: 6, name: "006", sex: true, age: 25 }, success: function (data) { var html = ''; $.each(data, function (index, value) { html += "<tr><td>" + value.Id + "</td><td>" + value.Name + "</td><td>" + value.Sex + "</td><td>" + value.Age + "</td></tr>"; }); $("#t").append(html); } });
请求过程变成这样

根本不是json,所以接口里根本接受不到数据,通过查资料发现,需要将上传的json对象序列化成json字符串,才可以
$.ajax({ url: "http://localhost:14788/api/test", type: "post", dataType: "json", contentType: "application/json", data: JSON.stringify({ id: 6, name: "006", sex: true, age: 25 }), success: function (data) { var html = ''; $.each(data, function (index, value) { html += "<tr><td>" + value.Id + "</td><td>" + value.Name + "</td><td>" + value.Sex + "</td><td>" + value.Age + "</td></tr>"; }); $("#t").append(html); } });

此时ok了,所以说在post的时候,有2种方式:
1、不指定contentType,使用默认的form提交
2、指定了contentType,就必须对json对象进行序列化
如果我不传递一个model,而是一个string呢,怎么办?
注:一个api里不许出现多个post,但是可以有多个get(get可以通过路由来区分,除非可以特定到某个方法可以定义多个post)
[HttpPost] public IEnumerable<string> Add2([FromBody]string a) { List<string> s = new List<string>(); s.Add(a); return s; }
调用
$.ajax({ url: "http://localhost:14788/api/test", type: "post", dataType: "json", data: { "": "school" }, success: function (data) { var html = ''; $.each(data, function (index, value) { html += value; }); $("#t").append(html); } });
很怪异是吧,没有key只有value,接口参数要价格FromBody,而且只有这一种通过post表单的方式去调用,
更怪异的是,如果想传递多个参数,绝对不是使用多个FromBody,而是使用 dynamic 关键词。
[HttpPost] public IEnumerable<string> Add(dynamic obj) { List<string> s = new List<string>(); s.Add(obj.Id.ToString()); s.Add(obj.Name.ToString()); s.Add(obj.Age.ToString()); return s; }
调用:
$.ajax({ url: "http://localhost:14788/api/test", type: "post", dataType: "json", contentType: "application/json", data: JSON.stringify({ Id: "009", Name: "school", Age: 26 }), success: function (data) { var html = ''; $.each(data, function (index, value) { html += value; }); $("#t").append(html); } });
此时只有用json的这一种方式(不能通过post调单提交),所有的key和接口里的dynamic的属性的名字大小写完全保持一致。
model和string混合使用post
[HttpPost] public IEnumerable<string> Add(dynamic obj) { List<string> s = new List<string>(); s.Add(obj.MM.ToString()); s.Add(obj.NN.Id.ToString()); s.Add(obj.NN.Name.ToString()); s.Add(obj.NN.Sex.ToString()); s.Add(obj.NN.Age.ToString()); return s; }
调用:
var temp = { Id: "7", Name: "007", Sex: true, Age: 28 } $.ajax({ url: "http://localhost:14788/api/test", type: "post", dataType: "json", contentType: "application/json", data: JSON.stringify({ MM: "009", NN: temp }), success: function (data) { var html = ''; $.each(data, function (index, value) { html += value; }); $("#t").append(html); } });
传递数组作为参数:
[HttpPost] public IEnumerable<string> Add(string[] obj) { List<string> s = new List<string>(); s.Add(obj[0].ToString()); s.Add(obj[1].ToString()); s.Add(obj[2].ToString()); return s; }
调用:
var temp = ["1", "2", "3"]; $.ajax({ url: "http://localhost:14788/api/test", type: "post", dataType: "json", contentType: "application/json", data: JSON.stringify(temp), success: function (data) { var html = ''; $.each(data, function (index, value) { html += value; }); $("#t").append(html); } });
以model集合post:
[HttpPost] public string Add(List<Person> list) { string str = ""; foreach (var item in list) { str += item.Name; } return str; }
调用:
var list = [{ Id: "7", Name: "007", Sex: true, Age: 28 }, { Id: "8", Name: "008", Sex: true, Age: 29 }]; $.ajax({ url: "http://localhost:14788/api/test", type: "post", dataType: "json", contentType: "application/json", data: JSON.stringify(list), success: function (data) { $("#t").append(data); } });
其实有了上面的那些post的详细情况的介绍后,就发现
put和delete的做法和post是一样的,就是ajax时的type不一样罢了,api不近支持web端的ajax,也支持 客户端(后台)的操作,可以说相当的完美。
浙公网安备 33010602011771号