概述
歡迎來到本文,本篇文章將會(huì)探討C# WebApi中傳遞參數(shù)的方法。在WebApi中,參數(shù)傳遞是一個(gè)非常重要的概念,因?yàn)樗沟梦覀兡軌驈目蛻舳双@取數(shù)據(jù),并將數(shù)據(jù)傳遞到服務(wù)器端進(jìn)行處理。WebApi是一種使用HTTP協(xié)議進(jìn)行通信的RESTful服務(wù),它可以通過各種方式傳遞參數(shù)。在本文中,我們只會(huì)針對(duì)Get和Post討論參數(shù)傳遞的方法,以及如何在C# WebApi中正確地處理它們。
Get
GET請(qǐng)求方法用于獲取資源,通常會(huì)將參數(shù)放在URL的查詢字符串中進(jìn)行傳遞。由于GET請(qǐng)求方法是無狀態(tài)的,因此它通常被用于獲取數(shù)據(jù),而不是修改數(shù)據(jù)。
// 該函數(shù)用于向服務(wù)器發(fā)送GET請(qǐng)求并獲取數(shù)據(jù)
export function getAction(url, query) {
return request({
url: url,
method: 'get',
params: query
})
}
1.傳遞字符串參數(shù)
// 前端代碼
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調(diào)用截圖
2.傳遞實(shí)體參數(shù)
注意:.Net Core 項(xiàng)目中使用[FromQuery]
特性,在.Net Framework 項(xiàng)目中使用[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調(diào)用截圖
Post
POST請(qǐng)求方法用于向服務(wù)器端提交數(shù)據(jù),通常會(huì)將參數(shù)放在請(qǐng)求體中進(jìn)行傳遞。POST請(qǐng)求方法通常被用于創(chuàng)建、更新或刪除資源。
// 該函數(shù)用于向服務(wù)器發(fā)送POST請(qǐng)求并獲取數(shù)據(jù)
export function postAction(url, data) {
return request({
url: url,
method: 'post',
data: data
})
}
1.傳遞實(shí)體參數(shù)
// 前端代碼
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調(diào)用截圖
2.傳遞實(shí)體集合參數(shù)
// 前端代碼
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調(diào)用截圖
3.傳遞數(shù)組參數(shù)
// 前端代碼
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調(diào)用截圖文章來源:http://www.zghlxwxcb.cn/news/detail-852152.html
# 概述
歡迎來到本文,本篇文章將會(huì)探討C# WebApi中傳遞參數(shù)的方法。在WebApi中,參數(shù)傳遞是一個(gè)非常重要的概念,因?yàn)樗沟梦覀兡軌驈目蛻舳双@取數(shù)據(jù),并將數(shù)據(jù)傳遞到服務(wù)器端進(jìn)行處理。WebApi是一種使用HTTP協(xié)議進(jìn)行通信的RESTful服務(wù),它可以通過各種方式傳遞參數(shù)。在本文中,我們只會(huì)針對(duì)Get和Post討論參數(shù)傳遞的方法,以及如何在C# WebApi中正確地處理它們。
Get
GET請(qǐng)求方法用于獲取資源,通常會(huì)將參數(shù)放在URL的查詢字符串中進(jìn)行傳遞。由于GET請(qǐng)求方法是無狀態(tài)的,因此它通常被用于獲取數(shù)據(jù),而不是修改數(shù)據(jù)。
// 該函數(shù)用于向服務(wù)器發(fā)送GET請(qǐng)求并獲取數(shù)據(jù)
export function getAction(url, query) {
return request({
url: url,
method: 'get',
params: query
})
}
1.傳遞字符串參數(shù)
// 前端代碼
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調(diào)用截圖
2.傳遞實(shí)體參數(shù)
注意:.Net Core 項(xiàng)目中使用[FromQuery]
特性,在.Net Framework 項(xiàng)目中使用[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調(diào)用截圖
Post
POST請(qǐng)求方法用于向服務(wù)器端提交數(shù)據(jù),通常會(huì)將參數(shù)放在請(qǐng)求體中進(jìn)行傳遞。POST請(qǐng)求方法通常被用于創(chuàng)建、更新或刪除資源。
// 該函數(shù)用于向服務(wù)器發(fā)送POST請(qǐng)求并獲取數(shù)據(jù)
export function postAction(url, data) {
return request({
url: url,
method: 'post',
data: data
})
}
1.傳遞實(shí)體參數(shù)
// 前端代碼
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調(diào)用截圖
2.傳遞實(shí)體集合參數(shù)
// 前端代碼
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調(diào)用截圖
3.傳遞數(shù)組參數(shù)
// 前端代碼
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調(diào)用截圖
文章來源地址http://www.zghlxwxcb.cn/news/detail-852152.html
到了這里,關(guān)于C# WebApi傳參及Postman調(diào)試的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!