C# WebApi传参及Postman调试

这篇具有很好参考价值的文章主要介绍了C# WebApi传参及Postman调试。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

概述

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

Get

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

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

1.传递字符串参数

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

附上Postman调用截图

c# 模拟postman body传参,C#,.NET Core,.NET Framework,c#,postman,lua

2.传递实体参数

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

// 前端代码
handleTest() {
      getAction('/test/getPerson', { Name: 'Hpf', Age: '29', Sex: '男' }).then((res) => {
        console.log('res=', res)
      })
    },
//后端代码
[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调用截图

c# 模拟postman body传参,C#,.NET Core,.NET Framework,c#,postman,lua

Post

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

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

1.传递实体参数

// 前端代码
handleTest() {
      postAction('/test/postPerson', { Name: 'Hpf', Age: '29', Sex: '男' }).then((res) => {
        console.log('res=', res)
      })
    },
// 后端代码
[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调用截图

c# 模拟postman body传参,C#,.NET Core,.NET Framework,c#,postman,lua

2.传递实体集合参数

// 前端代码
handleTest() {
      let list = [
        { Name: 'Hpf', Age: '29', Sex: '男' },
        { Name: 'Zzr', Age: '26', Sex: '女' },
      ]
      postAction('/test/postPerson', list).then((res) => {
        console.log('res=', res)
      })
    },
// 后端代码
[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调用截图

c# 模拟postman body传参,C#,.NET Core,.NET Framework,c#,postman,lua

3.传递数组参数

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

附上Postman调用截图

c# 模拟postman body传参,C#,.NET Core,.NET Framework,c#,postman,lua# 概述

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

Get

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

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

1.传递字符串参数

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

附上Postman调用截图

c# 模拟postman body传参,C#,.NET Core,.NET Framework,c#,postman,lua

2.传递实体参数

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

// 前端代码
handleTest() {
      getAction('/test/getPerson', { Name: 'Hpf', Age: '29', Sex: '男' }).then((res) => {
        console.log('res=', res)
      })
    },
//后端代码
[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调用截图

c# 模拟postman body传参,C#,.NET Core,.NET Framework,c#,postman,lua

Post

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

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

1.传递实体参数

// 前端代码
handleTest() {
      postAction('/test/postPerson', { Name: 'Hpf', Age: '29', Sex: '男' }).then((res) => {
        console.log('res=', res)
      })
    },
// 后端代码
[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调用截图

c# 模拟postman body传参,C#,.NET Core,.NET Framework,c#,postman,lua

2.传递实体集合参数

// 前端代码
handleTest() {
      let list = [
        { Name: 'Hpf', Age: '29', Sex: '男' },
        { Name: 'Zzr', Age: '26', Sex: '女' },
      ]
      postAction('/test/postPerson', list).then((res) => {
        console.log('res=', res)
      })
    },
// 后端代码
[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调用截图

c# 模拟postman body传参,C#,.NET Core,.NET Framework,c#,postman,lua

3.传递数组参数

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

附上Postman调用截图

c# 模拟postman body传参,C#,.NET Core,.NET Framework,c#,postman,lua文章来源地址https://www.toymoban.com/news/detail-852152.html

到了这里,关于C# WebApi传参及Postman调试的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Dynamics 365 设置Postman environment For WebAPI

         在官网看到这么一篇\\\"Set up a Postman environment\\\",不用在Azure AD中注册application就可以在postman中构建WebAPI,对于开发者来说确实能帮助我们更快的上手开发,但国内用的是21V,所以本篇就来记录下验证后在21V中的可用性。       首先根据博文中的描述,我先找了个galobal的环

    2024年02月11日
    浏览(35)
  • Postman传参的JSON格式

    实体类: json格式: 1.JSON数值({ “key” : value}) 2.JSON字符串({ “key” : “value”}) 3.JSON数组({ “key” : [value]}) 4.JSON对象({ “key” : {value}}) 5.JSON对象数组({ “key” : [{“key1”: “value1”},{“key2”: “value2”}]}) 6.JSON数组对象({“key”:{“key1”:[value1,value2]}})

    2024年02月12日
    浏览(32)
  • ApiPost/Postman 传参赋值详解

    当使用getMapping()时,使用@requestParam(\\\"strs\\\") ListString strs ApiPost 还有一种写法:     当使用PostMapping()时,使用requestBody    APIPost 即: 如果是ListInteger 详细说一下:    如果使用requestParam 注意 @RequestParam 里的 value 一定要带上中括号:   或者             ApiPost 多种情况:    

    2024年02月03日
    浏览(24)
  • Postman的FormData传参用法详解

            今年上半年因为做毕设的原因,有自己接触到后端,也是用过了postman去测试接口,看到了postman那边的参数形式,一直对这个formData有想法。         在做毕设前后端对接接口过程中,一般get或者delete请求我都会使用url拼接的形式,因为根据restAPI格式,这两者

    2024年02月11日
    浏览(32)
  • Postman传参是Json字符串

    进入Postman以后,点击Body,点击raw,将Json参数复制到空白框里,点击末尾,选择JSON格式。

    2024年02月11日
    浏览(35)
  • postman批量执行请求,通过json传参

    \\\"authUid\\\":\\\" {{authUid}} \\\", 加粗为需要替换的参数 可通过Excel自动填充功能构造数据 学习借鉴:https://www.cnblogs.com/l0923/p/13419986.html

    2024年02月17日
    浏览(33)
  • postman----传参格式(json格式、表单格式)

      本文主要讲解postman使用post请求方法的2中传参方式:json格式、表单格式 首先了解下,postman进行接口测试,必须条件是: ♥请求地址 ♥请求协议 ♥请求方式 ♥请求头 ♥参数 json格式 先看一下接口文档,根据接口文档,在postman中填入必要的参数信息 post请求方式,使用 

    2024年02月14日
    浏览(30)
  • 多个对象入参PostMan测试传参方式

    多个对象入参 使用postMan测试 但是怎么传值后端都接收不到数据  前言:因为需要写一个分页查询 但是组里现场的传值方式都是这种所以 就遇到了两个参数不知道怎么传值的情况 在这写了一个简单的方法测试PostMan怎么传值两个对象的情况  简单的测试 接口代码: 其中实体类

    2024年02月16日
    浏览(31)
  • web-view往h5传参及传参乱码问题

    1、微信端的操作 往wxml中配置web-view 并在对应js中动态设置路径的参数 在需要的地方修改其路径参数 2、h5端(接受上面传进来的参数) 注:这里建议如果h5是vue项目的话,可以本地映射一个地址出去。在vue.config.js下配置devServer--host设置为本机ip地址,接着小程序接入该地址用

    2024年02月09日
    浏览(26)
  • postman form-data传参java实现

    java实现: 第二种方式: 导入依赖:

    2024年02月12日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包