I am creating a Blazor WebAssembly app using .net 7 and I have the most bizzare problem. In my client I am making a call to the server. It's a really simple call to get a Room item from the database and it sends the ID of the room to the controller as part of a Get request:
public async Task<ServiceResponse<Room>> GetDetailedRoom(int roomId)
{
try
{
var response = await _http.GetFromJsonAsync<ServiceResponse<Room>>($"api/rooms/{roomId}");
return response;
}
catch (Exception ex)
{
return new ServiceResponse<Room> { Success = false, Message = ex.Message };
}
}
Most of the time it works. For example, if roomId is 36 then when the HttpClient call is made the Controller function is hit and then the database call it made and the room data gets returned and all is fine. However, when roomId is 37 the Controller function is never called and an exception is thrown with this error:
''<' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.'
In order to investigate the problem I changed my function to this so that I do the GetAsync call separately and then I read the response as a string so I can see it, and this is where it gets weird:
public async Task<string> GetDetailedRoom(int roomId)
{
try
{
string endpoint = $"api/rooms/{roomId}";
var response = await _http.GetAsync(endpoint);
if (response.IsSuccessStatusCode)
{
var content = response.Content;
var str = await content.ReadAsStringAsync();
return str;
}
return null;
}
catch (Exception ex)
{
return ex.Message;
}
}
When roomId 36 is used, the 'str' value contains the json for the room. Nothing unexpected there. But when roomId is 37, not only is the response code 200 'IsSuccessStatusCode' (even though it never hits the controller) this is what is getting returned:
'<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>MyApp</title>
<base href="/" />
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="css/app.css" rel="stylesheet" />
<link rel="icon" type="image/png" href="favicon.png" />
<link href="MyApp.Client.styles.css" rel="stylesheet" />
<link href="manifest.json" rel="manifest" />
<link rel="apple-touch-icon" sizes="512x512" href="icon-512.png" />
<link rel="apple-touch-icon" sizes="192x192" href="icon-192.png" />
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
<link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
</head>
<body>
<div id="app">
<svg class="loading-progress">
<circle r="40%" cx="50%" cy="50%" />
<circle r="40%" cx="50%" cy="50%" />
</svg>
<div class="loading-progress-text"></div>
</div>
<div id="blazor-error-ui">
An unhandled error has occurred.
<a href="" class="reload">Reload</a>
<a class="dismiss"></a>
</div>
<script src="_content/Blazor.Extensions.Canvas/blazor.extensions.canvas.js"></script>
<script src="js/canvas.js"></script>
<script src="_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js"></script>
<script src="_framework/blazor.webassembly.js"></script>
<script src="_content/MudBlazor/MudBlazor.min.js"></script>
<script>navigator.serviceWorker.register('service-worker.js');</script>
</body>
</html>
'
It's the entire contents of my index.html file from in my wwwroot folder! This is obviously why the GetFromJsonAsync is failing with the above error. The question is though, why is this happening? How on earth can the call work just fine with other ID numbers but then for number 37, instead of making a call to the controller, it decides to instead read some random file off disk and return its entire contents?
How can I go about finding out what it causing this to happen? Is there a way that I can view and step into the code for the GetAsync function?