0

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?

Simon Bailey
  • 41
  • 1
  • 4
  • Doesn't look like a random file. Rather, you have the `An unhandled error has occurred.` clearly put in the returning page which is a strong indication that something's wrong at the server and the server is configured to return a default error page which in turn crashes the client (as it doesn't expect a html error page returned for a json call). You either debug this server or put a huge try-catch over your server method and log whatever bad happens. My guess is that this particular record cannot be serialized for some reason (e.g. a circular dependency). – Wiktor Zychla Aug 04 '23 at 12:52
  • Hi Wiktor Zychla, I just wanted to thank you for taking the time to comment. However I have since resolved the problem and posted the solution below. – Simon Bailey Aug 04 '23 at 13:06

1 Answers1

0

I have just managed to resolve the problem. I tried changing the contents of the index.html file to see if the change got reflected in the response, and it didn't, so I got the feeling that something was being cached somewhere. So in Chrome I did a Ctrl + F5, but this didn't resolve it either, and neither did deleting all my bin + obj folders and rebooting and rebuilding the project.

The thing that resolved it was in Chrome (and specifically inside the window that the app is running in) I went to Settings (the 3 dots) -> More Tools -> Clear Browsing Data... and I wiped all Cached Images & Files.

The app now work perfectly. There must have been some dodgy file in the cache.

Simon Bailey
  • 41
  • 1
  • 4