3

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}")]
1
  • I believe that FromURI and HttpResponseMessage message are not used in .Net Core 2.0 - This is where I am at in my attempts - but this still doesn't work: [HttpGet("api/scoinfo/{activityId}/{studentId}/{timestamp}")] public async Task<IActionResult> GetLearningTask([FromRoute]int activityId, [FromRoute]int studentId, [FromRoute]int timeStamp) Commented May 26, 2018 at 22:26

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

1

Query parameters are not part of the route path. The route for https://localhost:44329/api/scoInfo?activityId=1&studentId=1&timestamp=1527357844844 would be "api/scoinfo" . The handling method would then have parameters decorated with [FromUri] which will map to the values in the query parameter.

[Route("api/scoinfo")]
public HttpResponseMessage GetScoInfo([FromUri]int activityId, int studentId, int timeStamp)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.