Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accept multiple parameters for API in .NET Core 2.0 using attribute routing

I have an incoming GET request as follows:

https://localhost:44329/api/scoInfo?activityId=1&studentId=1&timestamp=1527357844844

How would I create an endpoint in ASP.net-core that would accept this request.

I have tried different versions of the following and nothing works:

[HttpGet("{activityId: int}/{studentid: int}/{timestamp: int}")]
[Route("api/ScoInfo/{activityId}/{studentid}/{timestamp}")]
like image 722
Roddy Balkan Avatar asked May 08 '26 01:05

Roddy Balkan


1 Answers

Use [FromQuery] to specify the exact binding source you want to apply.

//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) {
    //...
}

MVC will try to bind request data to the action parameters by name. MVC will look for values for each parameter using the parameter name and the names of its public settable properties.

Reference Model Binding in ASP.NET Core

like image 66
Nkosi Avatar answered May 10 '26 15:05

Nkosi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!