asp.net.core webapi接收参数

 

数据请求的来源

1.url

[FromQuery] 来自于url

2.from表单

[FromForm]来自于表单数据

3.app/json

[FromBody] 来自于 json

4.restful风格

 

 

 

 

 

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. 参数是一个对象   

[FromQuery]特性

        [HttpGet]
        public string g3([FromQuery]Student student)
        {
            return student.name;
        }

不常见get里带body

        [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;
        }
        public class Student
        {
            public int Age { get; set; }
            public string name { get; set; }
        }

  

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

 

 

获取Head的值

方法参数前添加 [FromHeader]

Name=  固定

        [HttpGet]
        public string test(int a,[FromHeader(Name="qionghaha")] string? ha)
        {
            return ha;
        }

 

get post 占位符

http://localhost:5124/Test/test/2/3

        [HttpGet("{a1}/{a2}")]
        public int test(int a1,int a2)
        {
            return a1+a2;
        }

a1=2 a2=3

 

路由获取参数

 [FromRoute] 

        [HttpGet("aaa/{a1}/bbb/{a2}")]
        //获取地址里的a2重命名为xxx
        public int test(int a1,[FromRoute(Name = "a2")] int xxx)
        {
            return xxx;
        }

 

 通过参数进行依赖注入

[FromServices] 服务

    [Route("[controller]/[action]")]
    [ApiController]
    public class TestController : ControllerBase
    {

        [HttpGet]
        public int test([FromServices] Calculation calculation,int i)
        {
            return calculation.Add(2,3)*i;
        }
    }

注册

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();

builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<Calculation>();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();

app.Run();

public class Calculation
{
    public int Add(int a,int b)
    {
        return a + b;
    }
}

 

posted @ 2020-07-16 14:55  富坚老贼  阅读(1535)  评论(0编辑  收藏  举报