Blazor webapp and httpClient call API

prof cegep 41 Reputation points
2021-09-15T13:43:35.71+00:00

Hello all of you

In my dataservice (client-side) I try to call controller API with this (the Id 36fc1b86-9697-49c5-978c-825f465de73b is hard coded to show the problem) :

return await JsonSerializer.DeserializeAsync<IEnumerable<Stage>>
(await _httpClient.GetStreamAsync($"api/stage/36fc1b86-9697-49c5-978c-825f465de73b"), new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });

Server-side (API)

    [HttpGet("{id}")]
    public IActionResult GetAllStage(string id)
    {
        return Ok(_stageRepository.GetAllStagesById(id));
    }

And the error :

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Value cannot be null. (Parameter 'source')
System.ArgumentNullException: Value cannot be null. (Parameter 'source')

But if GUID starts with a letter (see below /a...) everything works #1 like this :

return await JsonSerializer.DeserializeAsync<IEnumerable<Stage>>
(await _httpClient.GetStreamAsync($"api/stage/a0bdb087-dadc-4382-9a28-75f93917698b"), new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });

Workaround. I put a extra letter (here X) at the very begining :

return await JsonSerializer.DeserializeAsync<IEnumerable<Stage>>
(await _httpClient.GetStreamAsync($"api/stage/X{id}"), new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });

And remove it in the API...

    [HttpGet("{id}")]
    public IActionResult GetAllStage(string id)
    {
        return Ok(_stageRepository.GetAllStagesById(id.Remove(0, 1)));
    }

Any idea about this bug?

Developer technologies | .NET | Blazor
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 30,491 Reputation points
    2021-09-15T14:35:37.737+00:00

    Use the actual Guid type rather than a string. You might also want to handle null.

    [HttpGet("{id:Guid?}")]
    public IActionResult Get(Guid? id)
    {
        return Ok(new { id = id ?? Guid.NewGuid() });
    }
    
    0 comments No comments

  2. prof cegep 41 Reputation points
    2021-09-16T00:28:09.033+00:00

    Helllo.

    Thanks for this information. Still not working.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.