Is is possible to return a nullable int with GetFromJsonAsync
? I'm getting a System.Text.Json.JsonException
error:
The input does not contain any JSON tokens
I have a simple API endpoint that retrieves an integer:
[HttpGet("BusinessId")]
public async Task<int?> BusinessId(int userId, CancellationToken token)
{
var businessId = await _businessService.GetDefaultBusinessId(userId, token);
return businessId;
}
I have a caller that uses GetFromJsonAsync
to retrieve the value:
public async Task<int?> GetDefaultBusinessId(int userId, CancellationToken token)
{
var url = $"https://localhost:7000/Business/BusinessId?userId={userId}";
var businessId = await client.GetFromJsonAsync<int?>(url, token);
return businessId;
}
GetFromJsonAsync
throws an exception when null is returned. Instead of returning zero, is it possible to return null with GetFromJsonAsync
?
UPDATE
This is a Blazor web app using InteractiveAuto as render mode.
UPDATE 2 Here's the response from the browser:
If I change GetFromJsonAsync to GetStringAsync, it returns an empty object:
In my API, I am adding Newtonsoft to my controllers via middleware. I believe, if I can remember, because of patching:
builder.Services.AddControllers(o =>
{
o.InputFormatters.Insert(0, KoJPIF.GetJsonPatchInputFormatter());
}).AddNewtonsoftJson();
Is is possible to return a nullable int with GetFromJsonAsync
? I'm getting a System.Text.Json.JsonException
error:
The input does not contain any JSON tokens
I have a simple API endpoint that retrieves an integer:
[HttpGet("BusinessId")]
public async Task<int?> BusinessId(int userId, CancellationToken token)
{
var businessId = await _businessService.GetDefaultBusinessId(userId, token);
return businessId;
}
I have a caller that uses GetFromJsonAsync
to retrieve the value:
public async Task<int?> GetDefaultBusinessId(int userId, CancellationToken token)
{
var url = $"https://localhost:7000/Business/BusinessId?userId={userId}";
var businessId = await client.GetFromJsonAsync<int?>(url, token);
return businessId;
}
GetFromJsonAsync
throws an exception when null is returned. Instead of returning zero, is it possible to return null with GetFromJsonAsync
?
UPDATE
This is a Blazor web app using InteractiveAuto as render mode.
UPDATE 2 Here's the response from the browser:
If I change GetFromJsonAsync to GetStringAsync, it returns an empty object:
In my API, I am adding Newtonsoft to my controllers via middleware. I believe, if I can remember, because of patching:
builder.Services.AddControllers(o =>
{
o.InputFormatters.Insert(0, KoJPIF.GetJsonPatchInputFormatter());
}).AddNewtonsoftJson();
Share
Improve this question
edited Jan 29 at 19:08
GH DevOps
asked Jan 29 at 16:58
GH DevOpsGH DevOps
4603 silver badges15 bronze badges
5
|
1 Answer
Reset to default 0I was able to get it working by adding an output formatter to my controller middleware that will omit the 204 No Content response code. I'm not sure if this is the best approach, but I'm trying to avoid returning an IActionResult in my controller:
builder.Services.AddControllers(o =>
{
o.InputFormatters.Insert(0, KoJPIF.GetJsonPatchInputFormatter());
o.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
}).AddNewtonsoftJson();
I found this workaround here: RemoveNoContent
GetFromJsonAsync
. Is the endpoint returning the literal stringnull
? No quotes, justnull
. – nbokmans Commented Jan 29 at 17:02GetFromJsonAsync
useGetStringAsync
to see the raw response string. I'm suspecting that the endpoint is not returning JSON but maybe HTML, perhaps due to aAccept
orContent-Type
header not being set. Which Swagger does set for you. – nbokmans Commented Jan 29 at 17:46