0

I am using Microsoft.AspNetCore.Blazor.HttpClient to perform requests on my API endpoints.

If the below call finds nothing it raises an exception.

using var client = auth.GetMyToken();
return await Task.Run(async () => await client.GetJsonAsync<MyModel>($"getsomething/{id}"));

The error says:

{"The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 0 | BytePositionInLine: 0."}

I am guessing it does this because the return is not JSON (it's nothing)

I'd rather not wrap a Try{} block around it to deal with no records found.

How can I amend this to return null (or an empty model) if no record is found?

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
levis84
  • 974
  • 1
  • 12
  • 27
  • 1
    Are you using the latest version of Blazor? It has switched to GetFromJsonAsync – bob82 Sep 21 '20 at 13:50
  • This a pre-release library so different to the one your are referring to I think: https://www.nuget.org/packages/Microsoft.AspNetCore.Blazor.HttpClient/ – levis84 Sep 21 '20 at 13:57
  • Define "finds nothing" please. – Aluan Haddad Sep 21 '20 at 14:11
  • So, if you make a GetById endpoint and you send in an id that does not exist. – levis84 Sep 21 '20 at 14:16
  • Can you share the exception details? – HMZ Sep 21 '20 at 14:23
  • 2
    3.2 was released so you should not need (and should not use) that package anymore. Update and see if this still is a question. – H H Sep 21 '20 at 14:44
  • Cool - updated to GetFromJsonAsync. It still errors with ""The provided ContentType is not supported; the supported types are 'application/json' and the structured syntax suffix 'application/+json'." - I am thinking this has more to do with the API returning null rather than an empty model? – levis84 Sep 21 '20 at 15:12
  • No, it is about your API not being found., – H H Sep 21 '20 at 15:42
  • See here: https://stackoverflow.com/a/63863447/60761 – H H Sep 21 '20 at 15:43
  • If you want this debugged then show the controller and action with all routing attributes. And the exact url you call it with. – H H Sep 21 '20 at 15:48
  • The API is working. Everything works fine if an ID is found and data is returned. – levis84 Sep 21 '20 at 16:02
  • @levis84 he's not saying that the API is the problem, the problem is that you're not reaching the api when using the httpclient and the provided url so blazor is returning html like when you call a normal url in the app and get a not found page. Check for the response with the browser dev tools. – HMZ Sep 21 '20 at 16:05
  • When it does work for some results then it might be about paramter validation. Again, post the routing details. – H H Sep 21 '20 at 17:06
  • Unrelated to the error: that `Task.Run()` is counter productive. Just don't do that. – H H Sep 21 '20 at 18:44
  • Yeh removed that bit - thanks – levis84 Sep 21 '20 at 19:04

2 Answers2

3

An empty result should not give an error.

The old Json API reports

The input does not contain any JSON tokens.

and the new one

The provided ContentType is not supported;

They are both caused by trying to parse HTML as Json. This is 'normal' (not good) for Blazor WebAssembly when the API is on the same host (as in the Hosted template).

Any error in the url for your API results in rendering the MainLayout with "sorry, nothing here" as the body. You will not get a 404.

So, debug your routing.

H H
  • 263,252
  • 30
  • 330
  • 514
2

Yes that is correct. Blazor GetFromJsonAsync will not handle empty Json result. Instead, you should do this when returning result in your web api controller:

public async Task<IActionResult> GetQuote(string quoteid)
    {
        int QuoteID = int.Parse(quoteid);
        var quote = await _dataContext.tblQuotes 
                            .Where(x => x.ID == QuoteID)
                            .SingleOrDefaultAsync();
                           
        if (quote == null) //DO NOT RETURN NULL
            quote = new Quote();
        return Ok(quote);

Notice here that if quote is null, I return a new Quote(), so the quote object is not null.

user3656651
  • 619
  • 1
  • 9
  • 25