最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - .NET 8 HttpClient-Return Nullable Int with GetFromJsonAsync - Stack Overflow

programmeradmin1浏览0评论

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 This should be doable by GetFromJsonAsync. Is the endpoint returning the literal string null? No quotes, just null. – nbokmans Commented Jan 29 at 17:02
  • What's the actual payload and what do you expect to get converted to a null? An empty body? – Panagiotis Kanavos Commented Jan 29 at 17:02
  • @nbokmans just null is returned. Swagger is returning zero. – GH DevOps Commented Jan 29 at 17:11
  • Interesting. Can you show the response of the GET request when done from the code, not Swagger UI? Perhaps instead of using GetFromJsonAsync use GetStringAsync to see the raw response string. I'm suspecting that the endpoint is not returning JSON but maybe HTML, perhaps due to a Accept or Content-Type header not being set. Which Swagger does set for you. – nbokmans Commented Jan 29 at 17:46
  • @nbokmans I updated my question. I'm not getting a JSON exception if I use GetStringAsync, just an empty object. I am using Netwonsoft instead of system.text.json in my API, not sure if that would cause the issue. – GH DevOps Commented Jan 29 at 19:10
Add a comment  | 

1 Answer 1

Reset to default 0

I 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

发布评论

评论列表(0)

  1. 暂无评论