0


C# WebApi传参及Postman调试

概述

欢迎来到本文,本篇文章将会探讨C# WebApi中传递参数的方法。在WebApi中,参数传递是一个非常重要的概念,因为它使得我们能够从客户端获取数据,并将数据传递到服务器端进行处理。WebApi是一种使用HTTP协议进行通信的RESTful服务,它可以通过各种方式传递参数。在本文中,我们只会针对Get和Post讨论参数传递的方法,以及如何在C# WebApi中正确地处理它们。

Get

GET请求方法用于获取资源,通常会将参数放在URL的查询字符串中进行传递。由于GET请求方法是无状态的,因此它通常被用于获取数据,而不是修改数据。

  1. // 该函数用于向服务器发送GET请求并获取数据exportfunctiongetAction(url, query){returnrequest({url: url,method:'get',params: query
  2. })}

1.传递字符串参数

  1. // 前端代码handleTest(){getAction('/test/list1',{id:1}).then((res)=>{
  2. console.log('res=', res)})},
  1. // 后端代码
  2. [Route("test")]
  3. public class TestController : ControllerBase
  4. {
  5. [HttpGet("list1")]
  6. public IActionResult Index(int id)
  7. {
  8. return Ok(id);
  9. }
  10. }

附上Postman调用截图

2.传递实体参数

注意:.Net Core 项目中使用

  1. [FromQuery]

特性,在.Net Framework 项目中使用

  1. [FromUri]

特性

  1. // 前端代码handleTest(){getAction('/test/getPerson',{Name:'Hpf',Age:'29',Sex:'男'}).then((res)=>{
  2. console.log('res=', res)})},
  1. //后端代码
  2. [Route("test")]
  3. public class TestController : BaseController
  4. {
  5. [HttpGet("getPerson")]
  6. public IActionResult GetPerson([FromQuery] Person person)
  7. {
  8. return Ok();
  9. }
  10. }
  11. public class Person
  12. {
  13. public string Name { get; set; }
  14. public string Age { get; set; }
  15. public string Sex { get; set; }
  16. }

附上Postman调用截图

Post

POST请求方法用于向服务器端提交数据,通常会将参数放在请求体中进行传递。POST请求方法通常被用于创建、更新或删除资源。

  1. // 该函数用于向服务器发送POST请求并获取数据exportfunctionpostAction(url, data){returnrequest({url: url,method:'post',data: data
  2. })}

1.传递实体参数

  1. // 前端代码handleTest(){postAction('/test/postPerson',{Name:'Hpf',Age:'29',Sex:'男'}).then((res)=>{
  2. console.log('res=', res)})},
  1. // 后端代码
  2. [Route("test")]
  3. public class TestController : BaseController
  4. {
  5. [HttpPost("postPerson")]
  6. public IActionResult PostPerson([FromBody] Person person)
  7. {
  8. return Ok();
  9. }
  10. }
  11. public class Person
  12. {
  13. public string Name { get; set; }
  14. public string Age { get; set; }
  15. public string Sex { get; set; }
  16. }

附上Postman调用截图

2.传递实体集合参数

  1. // 前端代码handleTest(){let list =[{Name:'Hpf',Age:'29',Sex:'男'},{Name:'Zzr',Age:'26',Sex:'女'},]postAction('/test/postPerson', list).then((res)=>{
  2. console.log('res=', res)})},
  1. // 后端代码
  2. [Route("test")]
  3. public class TestController : BaseController
  4. {
  5. [HttpPost("postPerson")]
  6. public IActionResult PostPerson([FromBody] List<Person> person)
  7. {
  8. return Ok();
  9. }
  10. }
  11. public class Person
  12. {
  13. public string Name { get; set; }
  14. public string Age { get; set; }
  15. public string Sex { get; set; }
  16. }

附上Postman调用截图

3.传递数组参数

  1. // 前端代码handleTest(){postAction('/test/postPerson',['1','2','3']).then((res)=>{
  2. console.log('res=', res)})},
  1. // 后端代码
  2. [Route("test")]
  3. public class TestController : BaseController
  4. {
  5. [HttpPost("postPerson")]
  6. public IActionResult PostPerson([FromBody] string[] str)
  7. {
  8. return Ok();
  9. }
  10. }

附上Postman调用截图

# 概述

欢迎来到本文,本篇文章将会探讨C# WebApi中传递参数的方法。在WebApi中,参数传递是一个非常重要的概念,因为它使得我们能够从客户端获取数据,并将数据传递到服务器端进行处理。WebApi是一种使用HTTP协议进行通信的RESTful服务,它可以通过各种方式传递参数。在本文中,我们只会针对Get和Post讨论参数传递的方法,以及如何在C# WebApi中正确地处理它们。

Get

GET请求方法用于获取资源,通常会将参数放在URL的查询字符串中进行传递。由于GET请求方法是无状态的,因此它通常被用于获取数据,而不是修改数据。

  1. // 该函数用于向服务器发送GET请求并获取数据exportfunctiongetAction(url, query){returnrequest({url: url,method:'get',params: query
  2. })}

1.传递字符串参数

  1. // 前端代码handleTest(){getAction('/test/list1',{id:1}).then((res)=>{
  2. console.log('res=', res)})},
  1. // 后端代码
  2. [Route("test")]
  3. public class TestController : ControllerBase
  4. {
  5. [HttpGet("list1")]
  6. public IActionResult Index(int id)
  7. {
  8. return Ok(id);
  9. }
  10. }

附上Postman调用截图

2.传递实体参数

注意:.Net Core 项目中使用

  1. [FromQuery]

特性,在.Net Framework 项目中使用

  1. [FromUri]

特性

  1. // 前端代码handleTest(){getAction('/test/getPerson',{Name:'Hpf',Age:'29',Sex:'男'}).then((res)=>{
  2. console.log('res=', res)})},
  1. //后端代码
  2. [Route("test")]
  3. public class TestController : BaseController
  4. {
  5. [HttpGet("getPerson")]
  6. public IActionResult GetPerson([FromQuery] Person person)
  7. {
  8. return Ok();
  9. }
  10. }
  11. public class Person
  12. {
  13. public string Name { get; set; }
  14. public string Age { get; set; }
  15. public string Sex { get; set; }
  16. }

附上Postman调用截图

Post

POST请求方法用于向服务器端提交数据,通常会将参数放在请求体中进行传递。POST请求方法通常被用于创建、更新或删除资源。

  1. // 该函数用于向服务器发送POST请求并获取数据exportfunctionpostAction(url, data){returnrequest({url: url,method:'post',data: data
  2. })}

1.传递实体参数

  1. // 前端代码handleTest(){postAction('/test/postPerson',{Name:'Hpf',Age:'29',Sex:'男'}).then((res)=>{
  2. console.log('res=', res)})},
  1. // 后端代码
  2. [Route("test")]
  3. public class TestController : BaseController
  4. {
  5. [HttpPost("postPerson")]
  6. public IActionResult PostPerson([FromBody] Person person)
  7. {
  8. return Ok();
  9. }
  10. }
  11. public class Person
  12. {
  13. public string Name { get; set; }
  14. public string Age { get; set; }
  15. public string Sex { get; set; }
  16. }

附上Postman调用截图

2.传递实体集合参数

  1. // 前端代码handleTest(){let list =[{Name:'Hpf',Age:'29',Sex:'男'},{Name:'Zzr',Age:'26',Sex:'女'},]postAction('/test/postPerson', list).then((res)=>{
  2. console.log('res=', res)})},
  1. // 后端代码
  2. [Route("test")]
  3. public class TestController : BaseController
  4. {
  5. [HttpPost("postPerson")]
  6. public IActionResult PostPerson([FromBody] List<Person> person)
  7. {
  8. return Ok();
  9. }
  10. }
  11. public class Person
  12. {
  13. public string Name { get; set; }
  14. public string Age { get; set; }
  15. public string Sex { get; set; }
  16. }

附上Postman调用截图

3.传递数组参数

  1. // 前端代码handleTest(){postAction('/test/postPerson',['1','2','3']).then((res)=>{
  2. console.log('res=', res)})},
  1. // 后端代码
  2. [Route("test")]
  3. public class TestController : BaseController
  4. {
  5. [HttpPost("postPerson")]
  6. public IActionResult PostPerson([FromBody] string[] str)
  7. {
  8. return Ok();
  9. }
  10. }

附上Postman调用截图

标签: c# postman lua

本文转载自: https://blog.csdn.net/Myzhouzhou/article/details/135665223
版权归原作者 DotNeter-Hpf 所有, 如有侵权,请联系我们删除。

“C# WebApi传参及Postman调试”的评论:

还没有评论