Web API & Balzor: Mixed topics


Troubleshooting

Ok Response arrives but content is missing

Problem

We try to consume a value from a Web API method from Blazor. The Response has the Ok status, but the content is always null even though we are sure that we are indeed sending a content back.

Here is an example of such a scenario (of course there are many ways Blazor can consume a Web API):

Lets say we want to get a result of the following type from a Web API method:

public class SomeClass
{
   public string Content;
}

Our Web API method which we want to consume from the Blazor side looks something like this:

[HttpGet]
[Route("SomeMethod")]
public ActionResult<SomeClass> SomeMethod()
{
	var pi= new SomeClass{ Content = "Some content to be consumed." };
	return Ok(pi);
}

And on the Blazor side we want to consume that Web API with a call like this:

var someObject = await Http.GetFromJsonAsync<SomeClass>("/api/SomeController/SomeMethod");

The result is received normally but for some reason the Content-Property is always null.

Potential Resolution

The class of the returned result must have its properties declared properly!

public class SomeClass
{
    public string Content {get; set;}
}