Get方法
1.无参数
https://localhost:5001/test/g1
[ApiController] [Route("[controller]/[action]")] public class TestController : ControllerBase { [HttpGet] public string g1() { return "abc"; } }
2.两个参数
https://localhost:5001/test/g2?a=3&str=abc
[HttpGet] public string g2(int a,string str) { return a.ToString()+str; }
3. 参数是一个对象
[HttpGet] public string g3(Student student) { return student.name; }
对象
public class Student { public int age { get; set; } public string name { get; set; } }
body的 raw
{"age":18,"name":"tom"}
5.参数是集合
[HttpGet] public string g4(List<Student> list) { string str = ""; foreach (var stu in list) { str += stu.name; } return str; }
[{"age":18,"name":"tom"},{"age":22,"name":"liu"}]
Post
action默认就是post请求,可以不加[HttpPost]
1. 无参数
[HttpPost] public string p1() { return "abc"; }
2.一个参数
[HttpPost] public string p2([FromBody]string str) { return str; }
3.传对象
[HttpPost] public string p3([FromBody] Student student) { return student.name; }
4.数组
["ab","cd"]
[HttpPost] public string p4(string[] arr) { string str = ""; foreach (var a in arr) { str += a; } return str; }
5.传集合对象
[{"age":18,"name":"tom"},{"age":22,"name":"liu"}]
[HttpPost] public string p5(List<Student> list) { string str = ""; foreach (var stu in list) { str += stu.name; } return str; }
6.传对象和参数混合
参数放到url里,
[HttpPost] public string p6(List<Student> list,string a,string b) { string str = ""; foreach (var stu in list) { str += stu.name; } return str+a+b; }
同时
7.dynamic
{"age":18,"name":"tom"}
public string p7(dynamic obj) { var str = Convert.ToString(obj); Student student = JsonConvert.DeserializeObject<Student>(str); return student.name; }
[{"age":18,"name":"tom"},{"age":22,"name":"liu"}]
public string p7(dynamic obj) { var str = Convert.ToString(obj); List<Student> studentList = JsonConvert.DeserializeObject<List<Student>>(str); string res = ""; foreach (var student in studentList) { res += student.name; } return res; }
8.JObject
9.传表单
JDynamic