admin管理员组文章数量:1418380
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
本文标签: cNET 8 HttpClientReturn Nullable Int with GetFromJsonAsyncStack Overflow
版权声明:本文标题:c# - .NET 8 HttpClient-Return Nullable Int with GetFromJsonAsync - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745288530a2651648.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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