0

I have a controller with the following route:

[HttpGet("/{id}/studentrank")]
public async Task<ActionResult> GetStudentRank(Guid id){
...
}

Note, the decorator over the class is: [Route("api/[controller]")]

so it should be called by: api/student/{guid here}/studentrank

This works fine in swagger. However when I call it as follow, I get an internal server error and does not even break in the controller:

var response = await HttpClient.GetAsync($"/api/student/{id}/studentrank");

Any idea of what could be missing?

avdeveloper
  • 449
  • 6
  • 30
  • Attach a debugger and read the server output. Or enable dev exceptions and see the description directly. And a couple of things are missing in the question: We don't know whether this action is in StudentController, you say so only; we also don't know what type `id` is, it might not be a guid. – Tanveer Badar May 03 '21 at 10:16
  • 1
    Have you set a BaseAddress on the HttpClient? https://stackoverflow.com/questions/20609118/httpclient-with-baseaddress – monty May 03 '21 at 10:17
  • What is the body of the response. – tymtam May 03 '21 at 10:18

2 Answers2

0

The HttpGet does not contain the route parameter. Assuming the method is in the StudentController class you should do it like this:

[HttpGet]
[Route("GetStudentRank/{id}")]
public async Task<ActionResult> GetStudentRank(Guid id){
...
}

var response = await HttpClient.GetAsync($"{BaseUrl}/Student/GetStudentRank/{id}");
Paul Sinnema
  • 2,534
  • 2
  • 20
  • 33
0

I think, cause it is a Guid and not a string the type must be declared in the route and or the route must not start with a slash if it should be combined with the route on the class:

[HttpGet("{id:guid}/studentrank")]
public async Task<ActionResult> GetStudentRank(Guid id){
...
}
Oliver
  • 43,366
  • 8
  • 94
  • 151