• web api 接收参数


    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

  • 相关阅读:
    ASC1 C New Year Bonus Grant
    CodeForces 180C Letter
    Codeforces 71C Round Table Knights
    GCPC2014 J Not a subsequence
    treeview自动从表中添加标题和列值做目录的方法2
    delphi学习treeview中从表列名和数据添加为目录并双击自动选中
    对一个表中所有列数据模糊查询adoquery
    单击dbgrid列标题排序 升降序
    从右键菜单里粘贴复制的工资到数据库里
    如何从右键弹出菜单中清空删除数据加清空前提问确定
  • 原文地址:https://www.cnblogs.com/buchizaodian/p/13322658.html
Copyright © 2020-2023  润新知