Hello Hakan Arlı ,
From your screenshot, the controller is calling HttpClient, that call returns 404, and then your server throws when EnsureSuccessStatusCode
runs, so the browser shows 500. In short: the real problem is a bad URL or route mismatch in the HttpClient request, and your API is not handling that failure.
Try this quick checklist:
- Verify the real route
- Hit the API directly in the browser/Postman:
https://localhost:7156/projects
. - If it’s 404 there, check your controller routing. For attribute routes, make sure they line up, e.g.:
-
[Route("api/[controller]")]
+[HttpGet]
→/api/projects
-
[ApiController][Route("[controller]")]
+[HttpGet("projects")]
→/projects
-
- Ensure
app.MapControllers()
(or minimal API mappings) are inProgram.cs
.
- Hit the API directly in the browser/Postman:
- Fix the HttpClient URL construction
- If
BaseAddress
is set, you should pass only the relative path (no string interpolation with the base), e.g.:var client = _httpClientFactory.CreateClient("Api"); // has BaseAddress = https://localhost:7156 var response = await client.GetAsync("projects"); // not $"{BaseAddress}/projects"
- Double‑check port and scheme (http vs https) to match what Kestrel actually listens on.
- If
- Don’t convert a 404 into a 500
- Avoid
EnsureSuccessStatusCode()
unless you really want to throw. Handle not‑found explicitly and return something appropriate:var client = _httpClientFactory.CreateClient("Api"); var response = await client.GetAsync("projects"); if (response.StatusCode == HttpStatusCode.NotFound) return NotFound(); // or an empty list, as you prefer if (!response.IsSuccessStatusCode) return StatusCode((int)response.StatusCode, await response.Content.ReadAsStringAsync()); var projects = await response.Content.ReadFromJsonAsync<List<ProjectDto>>(); return Ok(projects);
- If you prefer one‑liner deserialization, avoid the throw:
var projects = await response.Content.ReadFromJsonAsync<List<ProjectDto>>();
- Avoid
- Add temporary diagnostics
- Log the final request URI and status code:
_logger.LogInformation("Requesting {Uri}", response.RequestMessage!.RequestUri); _logger.LogInformation("Status {Code}", (int)response.StatusCode);
- In
Program.cs
, enable detailed errors for local dev if needed.
- Log the final request URI and status code:
- Common routing pitfalls to check
- Mixed routes where Swagger shows
/api/projects
but your client calls/projects
. - Missing
[ApiController]
or route attributes on the controller/action. - Extra path base or reverse proxy prefix not accounted for.
- CORS is not your issue here; a 404 reached your server.
- Mixed routes where Swagger shows
If you share the controller attributes and the HttpClient setup (named client config and the line that builds the URL), it’ll be straightforward to pinpoint the exact mismatch.
Hope this helps you resolve it quickly!