c# - “HttpClient”不包含 'GetFromJsonAsync' 的定义

标签 c# .net blazor

我新建了一个 西装外套 应用程序,我想通过 访问 URL HttpClient
我遇到了这个错误

'HttpClient' does not contain a definition for 'GetFromJsonAsync' and no accessible extension method 'GetFromJsonAsync' accepting a first argument of type 'HttpClient' could be found (are you missing a using directive or an assembly reference?) [BlazorApp]csharp(CS1061) ,


这是我的代码
 @inject HttpClient Http
    <h1>@name</h1>
    @code {
        private string name;
    
        protected override async Task OnInitializedAsync()
        {
            name = await Http.GetFromJsonAsync<string>("api/");
        }
    }

最佳答案

您需要添加以下 using 指令:

using System.Net.Http.Json;
GetFromJsonAsync is an extension method ,不属于 HttpClient本身。

关于c# - “HttpClient”不包含 'GetFromJsonAsync' 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64134706/

相关文章:

c# - 如果可能,将多个 if 条件合并为单个 LINQ 语句

javascript - 检查 data-val-required 是否存在

c# - 如何制作使用 oninput 而不是 onchange 绑定(bind)的 EditForm 输入?

radio-button - 如何在单选按钮 Blazor 子组件上进行双向绑定(bind)

.net - 标签不显示 "_"字符

c# - 删除动态创建的组件总是从列表中删除最后一项

c# - 如何使用相对路径将本地 *.html 文件加载到 WPF Web 浏览器

c# - 我可以在 asp.net 文本框失去焦点之前触发文本更改事件吗?

c# - 使用 restsharp 反序列化 json 字符串

c# - 如何从线程数组中杀死特定线程