C# WebApi传参及Postman调试

概述

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

Get

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

javascript 复制代码
// 该函数用于向服务器发送GET请求并获取数据
export function getAction(url, query) {
  return request({
    url: url,
    method: 'get',
    params: query
  })
}

1.传递字符串参数

javascript 复制代码
// 前端代码
handleTest() {
      getAction('/test/list1', { id: 1 }).then((res) => {
        console.log('res=', res)
      })
    },
c# 复制代码
// 后端代码
[Route("test")]
public class TestController : ControllerBase
{
    [HttpGet("list1")]
    public IActionResult Index(int id)
    {
        return Ok(id);
    }
}

附上Postman调用截图

2.传递实体参数

注意:.Net Core 项目中使用[FromQuery]特性,在.Net Framework 项目中使用[FromUri]特性

javascript 复制代码
// 前端代码
handleTest() {
      getAction('/test/getPerson', { Name: 'Hpf', Age: '29', Sex: '男' }).then((res) => {
        console.log('res=', res)
      })
    },
c# 复制代码
//后端代码
[Route("test")]
public class TestController : BaseController
{
	[HttpGet("getPerson")]
	public IActionResult GetPerson([FromQuery] Person person)
	{
		return Ok();
	}
}


public class Person
{
	public string Name { get; set; }
	public string Age { get; set; }
	public string Sex { get; set; }
}

附上Postman调用截图

Post

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

javascript 复制代码
// 该函数用于向服务器发送POST请求并获取数据
export function postAction(url, data) {
  return request({
    url: url,
    method: 'post',
    data: data
  })
}

1.传递实体参数

javascript 复制代码
// 前端代码
handleTest() {
      postAction('/test/postPerson', { Name: 'Hpf', Age: '29', Sex: '男' }).then((res) => {
        console.log('res=', res)
      })
    },
c# 复制代码
// 后端代码
[Route("test")]
public class TestController : BaseController
{
	[HttpPost("postPerson")]
	public IActionResult PostPerson([FromBody] Person person)
	{
		return Ok();
	}
}


public class Person
{
	public string Name { get; set; }
	public string Age { get; set; }
	public string Sex { get; set; }
}

附上Postman调用截图

2.传递实体集合参数

javascript 复制代码
// 前端代码
handleTest() {
      let list = [
        { Name: 'Hpf', Age: '29', Sex: '男' },
        { Name: 'Zzr', Age: '26', Sex: '女' },
      ]
      postAction('/test/postPerson', list).then((res) => {
        console.log('res=', res)
      })
    },
c# 复制代码
// 后端代码
[Route("test")]
public class TestController : BaseController
{
	[HttpPost("postPerson")]
	public IActionResult PostPerson([FromBody] List<Person> person)
	{
		return Ok();
	}
}


public class Person
{
	public string Name { get; set; }
	public string Age { get; set; }
	public string Sex { get; set; }
}

附上Postman调用截图

3.传递数组参数

javascript 复制代码
// 前端代码
handleTest() {
      postAction('/test/postPerson',  ['1', '2', '3']).then((res) => {
        console.log('res=', res)
      })
    },
c# 复制代码
// 后端代码
[Route("test")]
public class TestController : BaseController
{
	[HttpPost("postPerson")]
	public IActionResult PostPerson([FromBody] string[] str)
	{
		return Ok();
	}
}

附上Postman调用截图

# 概述

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

Get

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

javascript 复制代码
// 该函数用于向服务器发送GET请求并获取数据
export function getAction(url, query) {
  return request({
    url: url,
    method: 'get',
    params: query
  })
}

1.传递字符串参数

javascript 复制代码
// 前端代码
handleTest() {
      getAction('/test/list1', { id: 1 }).then((res) => {
        console.log('res=', res)
      })
    },
c# 复制代码
// 后端代码
[Route("test")]
public class TestController : ControllerBase
{
    [HttpGet("list1")]
    public IActionResult Index(int id)
    {
        return Ok(id);
    }
}

附上Postman调用截图

2.传递实体参数

注意:.Net Core 项目中使用[FromQuery]特性,在.Net Framework 项目中使用[FromUri]特性

javascript 复制代码
// 前端代码
handleTest() {
      getAction('/test/getPerson', { Name: 'Hpf', Age: '29', Sex: '男' }).then((res) => {
        console.log('res=', res)
      })
    },
c# 复制代码
//后端代码
[Route("test")]
public class TestController : BaseController
{
	[HttpGet("getPerson")]
	public IActionResult GetPerson([FromQuery] Person person)
	{
		return Ok();
	}
}


public class Person
{
	public string Name { get; set; }
	public string Age { get; set; }
	public string Sex { get; set; }
}

附上Postman调用截图

Post

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

javascript 复制代码
// 该函数用于向服务器发送POST请求并获取数据
export function postAction(url, data) {
  return request({
    url: url,
    method: 'post',
    data: data
  })
}

1.传递实体参数

javascript 复制代码
// 前端代码
handleTest() {
      postAction('/test/postPerson', { Name: 'Hpf', Age: '29', Sex: '男' }).then((res) => {
        console.log('res=', res)
      })
    },
c# 复制代码
// 后端代码
[Route("test")]
public class TestController : BaseController
{
	[HttpPost("postPerson")]
	public IActionResult PostPerson([FromBody] Person person)
	{
		return Ok();
	}
}


public class Person
{
	public string Name { get; set; }
	public string Age { get; set; }
	public string Sex { get; set; }
}

附上Postman调用截图

2.传递实体集合参数

javascript 复制代码
// 前端代码
handleTest() {
      let list = [
        { Name: 'Hpf', Age: '29', Sex: '男' },
        { Name: 'Zzr', Age: '26', Sex: '女' },
      ]
      postAction('/test/postPerson', list).then((res) => {
        console.log('res=', res)
      })
    },
c# 复制代码
// 后端代码
[Route("test")]
public class TestController : BaseController
{
	[HttpPost("postPerson")]
	public IActionResult PostPerson([FromBody] List<Person> person)
	{
		return Ok();
	}
}


public class Person
{
	public string Name { get; set; }
	public string Age { get; set; }
	public string Sex { get; set; }
}

附上Postman调用截图

3.传递数组参数

javascript 复制代码
// 前端代码
handleTest() {
      postAction('/test/postPerson',  ['1', '2', '3']).then((res) => {
        console.log('res=', res)
      })
    },
c# 复制代码
// 后端代码
[Route("test")]
public class TestController : BaseController
{
	[HttpPost("postPerson")]
	public IActionResult PostPerson([FromBody] string[] str)
	{
		return Ok();
	}
}

附上Postman调用截图

相关推荐
光头闪亮亮25 分钟前
电子发票解析工具-c#桌面应用开发-DataGridView表格控件使用详解
c#
周杰伦fans1 小时前
C# 中的 `Hashtable`
开发语言·c#
lingggggaaaa1 小时前
免杀对抗——C2远控篇&PowerShell&有无文件落地&C#参数调用&绕AMSI&ETW&去混淆特征
c语言·开发语言·笔记·学习·安全·microsoft·c#
咩图2 小时前
WPF+Prism8.0.0.1909+C#创建一个桌面程序
c#·wpf·prism
Charles_go3 小时前
C#中级45、什么是组合优于继承
开发语言·c#
我是唐青枫3 小时前
一文理解 C#.NET Tuples:从基础到高级应用
c#·.net
Charles_go4 小时前
C#中级46、什么是模拟
开发语言·oracle·c#
一只爱做笔记的码农4 小时前
【BootstrapBlazor】移植BootstrapBlazor VS工程到Vscode工程,报error blazor106的问题
笔记·学习·c#
12程序猿5 小时前
postman调用文件(.xlsm---带宏的excel文件)下载接口成功下载excel文件,浏览器访问下载文件打不开
excel·lua·postman
霜绛6 小时前
Unity:lua热更新(三)——Lua语法(续)
unity·游戏引擎·lua