当前位置: 首页 > 工具软件 > CoreHTTP > 使用案例 >

Dotnet Core之[HttpGet]多参数

呼延晋
2023-12-01

**

Dotnet Core之[HttpGet]多参数

**

1、用FromRouteAttribute

//GET method
//single parameter
public IActionResult Get(int id)

//multiple parameter
[HttpGet("{id}/{backendOnly}")]
public IActionResult Get(int id, string backendOnly)

//multiple parameters
[HttpGet("{id}/{first}/{second}")]
public IActionResult Get(int id, string first, string second)

在浏览器中用route data发送GET request
https://localhost:8888/2
https://localhost:8888/2/first
https://localhost:8888/2/first/second

2、fromquery

//GET method
//single parameter
[HttpGet("details")]
public IActionResult Details(int id)

//multiple parameter
[HttpGet("details")]
public IActionResult Details(int id, string first)

//multiple parameters
[HttpGet("details")]
public IActionResult Details(int id, string first, string second)

在浏览器中通过fromquery发送get请求
//GET request using query parameters
https://localhost:8888/details?id=2
https://localhost:8888/details?id=2&&first=csharp
https://localhost:8888/details?id=2&&first=csharp&&second=mvc

3、Model Binding Sources
BindProperties attribute—This can be applied to class level to define the all properties need to map with HTTP request

// Model as parameter
[BindProperties]
public class GetRequest
{
public int Id { get; set; }
public string FrontEnd { get; set; }
public string BackEnd { get; set; }
}
// GET method
[HttpGet]
public IActionResult GetAction(GetRequest getRequest)

在浏览器发送get请求
https://localhost:8888/GetAction?id=2&&FrontEnd=csharp&&BackEnd=mvc ?

我个人比较推荐fromquery方式,这样可以很明确的知道调用哪个函数

参考:https://www.telerik.com/blogs/how-to-pass-multiple-parameters-get-method-aspnet-core-mvc

 类似资料: