使用属性路由接受 .NET Core 2.0 中 API 的多个参数

Rod*_*kan 3 c# asp.net-core asp.net-core-webapi

我有一个传入的 GET 请求,如下所示:

https://localhost:44329/api/scoInfo?activityId=1&studentId=1&timestamp=1527357844844
Run Code Online (Sandbox Code Playgroud)

我将如何在 ASP.net-core 中创建一个接受此请求的端点。

我尝试了以下不同版本,但没有任何效果:

[HttpGet("{activityId: int}/{studentid: int}/{timestamp: int}")]
[Route("api/ScoInfo/{activityId}/{studentid}/{timestamp}")]
Run Code Online (Sandbox Code Playgroud)

Nko*_*osi 5

使用[FromQuery]指定要应用精确的绑定源。

//GET api/scoInfo?activityId=1&studentId=1&timestamp=1527357844844
[HttpGet("api/scoinfo")] 
public async Task<IActionResult> GetLearningTask(
    [FromQuery]int activityId, 
    [FromQuery]int studentId, 
    [FromQuery]int timeStamp) {
    //...
}
Run Code Online (Sandbox Code Playgroud)

MVC 将尝试按名称将请求数据绑定到操作参数。MVC 将使用参数名称及其公共可设置属性的名称查找每个参数的值。

ASP.NET Core 中的参考模型绑定