C
C#15mo ago
alkasel#159

✅ Controller method can be invoked from Edge and Postman but not by Firefox

Hi, I've the following server controller:
[Route("[controller]")]
[ApiController]
public class ProductionOrderController : ControllerBase
{
// ...

[HttpGet("GetCount")]
public async Task<ActionResult<string>> GetCount() {
try {
int result = await _productionOrderDAL.GetCountAsync();
return result.ToString();
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
return new StatusCodeResult((int)HttpStatusCode.InternalServerError);
}
}
}
[Route("[controller]")]
[ApiController]
public class ProductionOrderController : ControllerBase
{
// ...

[HttpGet("GetCount")]
public async Task<ActionResult<string>> GetCount() {
try {
int result = await _productionOrderDAL.GetCountAsync();
return result.ToString();
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
return new StatusCodeResult((int)HttpStatusCode.InternalServerError);
}
}
}
Client side (blazor wasm) I invoke such method as follows:
private async Task MyMethod() {
try {
Task<HttpResponseMessage> t1 = _httpClient.GetAsync("ProductionOrder/GetCount");
HttpResponseMessage response = await t1;
string countStr = await response.Content.ReadAsStringAsync();
int count = int.Parse(countStr);

// ...

} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
private async Task MyMethod() {
try {
Task<HttpResponseMessage> t1 = _httpClient.GetAsync("ProductionOrder/GetCount");
HttpResponseMessage response = await t1;
string countStr = await response.Content.ReadAsStringAsync();
int count = int.Parse(countStr);

// ...

} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
If I launch the server and edge client, the latter retrieves the integer value. Same with Postman. Using Firefox, on the other hand, I have that the request is sent, the controller returns the correct integer value (I can see that debugging) but it seems like the client is not receiving the answer: from developer tools, there is no status code as well as response header. The "response" tab in developers tool shows the client index.html page with an error:
<!DOCTYPE html>
...
<body>
<div id="blazor-error-ui">
An unhandled error has occurred.
<a href="" class="reload">Reload</a>
</div>
</body>
...
<!DOCTYPE html>
...
<body>
<div id="blazor-error-ui">
An unhandled error has occurred.
<a href="" class="reload">Reload</a>
</div>
</body>
...
And of course the parsing fails. Please note that the controller has another method, a get request returning a list of items, and in that case firefox client retrieves the list of items successfully. Could you please give me a hand?
3 Replies
rhymbit
rhymbit15mo ago
The only reason it might be failing in firefox is because firefox is the only browser these days that checks SSL certs properly and you might be trying to visit the https url of your api in firefox and I think only VS and Rider will generate a temp SSL cert for your localhost api url while you're developing the app. using VsCode or just the terminal dotnet run won't work so instead, just use the http urls catshrug
Accord
Accord15mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
alkasel#159
alkasel#15915mo ago
Now it works fine, after the latest package update I suppose... I'm not sure. It could be due to cache cleaning as well, but I performed it several time, so I don't think that's the reason. I was already using http by the way, but thank you for the suggestion anyway