gpt4 book ai didi

How do I get this call to httpClient.GetFromJsonAsync() to work?(我如何让这个对httpClient.GetFromJsonAsync()的调用起作用?)

转载 作者:bug小助手 更新时间:2023-10-25 22:58:33 26 4
gpt4 key购买 nike



I have two similar calls on a Blazor page:

我在Blazor的页面上有两个类似的电话:


private async void SelectQuiz()
{
List<String>? ls = await httpClient.GetFromJsonAsync<List<String>>("api/BgQuizAnswer/GetBgQuizList");
String? txt = await httpClient.GetFromJsonAsync<String> ("api/BgQuizAnswer/GetHello");

The first call works fine while the second one fails.

第一个调用运行正常,而第二个调用失败。


        [HttpGet]
[Route("GetBgQuizList")]
public ActionResult<List<String>> GetBgQuizList()
{
return _bgQuizAnswerService.GetBgQuizList();
}
[HttpGet]
[Route("GetHello")]
public ActionResult<String> GetHello()
{
return "Hello";
}

Here's the start of the error messages I get when I inspect the page:


Unhandled Exception:

System.Text.Json.JsonException: 'H' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.

---> System.Text.Json.JsonReaderException: 'H' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0.


Note that the 'H' is the first letter of the return string. If I change 'Hello' to 'Jello', the error complains about 'J'.

下面是我在检查页面时收到的错误消息的开头:未处理的异常:System.Text.Json.JsonException:‘h’是无效的值开头。路径:$|线号:0|BytePositionInLine:0。->System.Text.Json.JsonReaderException:‘h’是无效的值开头。LineNumber:0|BytePositionInLine:0。请注意,‘H’是返回字符串的第一个字母。如果我将‘Hello’更改为‘Jello’,则错误提示‘J’。


更多回答
优秀答案推荐

You're trying to retrieve content as JSON, but your API doesn't return it as JSON. Thus you need to change your GetHello method to correct this:

您试图以JSON形式检索内容,但您的API没有将其作为JSON返回。因此,您需要更改GetHello方法以更正此错误:


        [HttpGet]
[Route("GetHello")]
public ActionResult<String> GetHello()
{
return Json("Hello");
}

Why is this not necessary for GetBgQuizList? Because ASP.NET Core serialises complex types like lists to JSON as default.

为什么这对于GetBgQuizList不是必需的?因为ASP.NET Core默认将复杂类型(如列表)序列化为JSON。



A valid JSON should be enclosed with {...} (braces) which represents an object or [...] (square brackets) which represents an array.

有效的JSON应用表示对象或[...]的{...}(大括号)括起来(方括号),表示数组。


The API for the second HttpClient call returns a string.

第二个HttpClient调用的API返回一个字符串。


You should use GetStringAsync to get the string value.

您应该使用GetStringAsync来获取字符串值。


String? txt = await httpClient.GetStringAsync("api/BgQuizAnswer/GetHello");


Replacing

return "Hello";

with

return JsonSerializer.Serialize("Hello");

works.


Replacing

String? txt = await httpClient.GetFromJsonAsync ("api/BgQuizAnswer/GetHello");

with

String? txt = await httpClient.GetStringAsync("api/BgQuizAnswer/GetHello");

also works.

将返回“Hello”;替换为Return JsonSerializer.Serialize(“Hello”);起作用。替换字符串?Txt=等待httpClient.GetFromJsonAsync(“API/BgQuizAnswer/GetHello”);带字符串?Txt=等待httpClient.GetStringAsync(“api/BgQuizAnswer/GetHello”);也可以工作。


更多回答

This gives the follow compile error: CS0103 The name 'Json' does not exist in the current context

这会导致以下编译错误:CS0103名称‘json’在当前上下文中不存在

I'm not sure what to tell you, it's a built-in method on all controllers: learn.microsoft.com/dotnet/api/…

我不知道该怎么说,这是所有控制器上的内置方法:learn.microsoft.com/dotnet/api/…

Error CS0103 The name 'Json' does not exist in the current context

错误CS0103名称‘Json’在当前上下文中不存在

A valid JSON should be enclosed with {...} (braces) which represents an object or [...] (square brackets) which represents an array. - this was true with RFC-4627 but is no longer true with RFC-8259, which allows the root JSON value to be a primitive. See What is the minimum valid JSON?.

有效的JSON应用表示对象或[...]的{...}(大括号)括起来(方括号),表示数组。-这适用于RFC-4627,但不再适用于允许根JSON值为原语的RFC-8259。请参阅最低有效JSON是多少?

@dbc - yes, it is allowed to be "Hello" but it now is just Hello.

@dbc-是的,可以是“Hello”,但现在只是Hello。

@HH - right, that's the problem exactly.

@HH-对,这就是问题所在。

This doesn't work: return "{Hello}";

这不起作用:返回“{Hello}”;

String? txt = await httpClient.GetStringAsync<String>("api/BgQuizAnswer/GetHello"); gives this compile error: CS0308 The non-generic method 'HttpClient.GetStringAsync(string?)' cannot be used with type arguments

细绳?Txt=await httpClient.GetStringAsync(“api/BgQuizAnswer/GetHello”);出现此编译错误:CS0308非泛型方法‘HttpClient.GetStringAsync(字符串?)’不能与类型参数一起使用

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com