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

c# - CS0117 - 'HttpClient' does not contain a definition for 'GetFromJsonAsync' - Stack Overflow

programmeradmin0浏览0评论

I'm following the Microsoft tutorial on Blazor. I'm getting the 'HttpClient doesn't contain a definition for 'GetFromJsonAsync' error when trying to use this extension method in the Index.razor page. System.Net.Http.Json (v. 6.0.0) is installed.

This is what my Index.razor file looks like:

@page "/"

@using System.Net.Http.Json

<div class="main">
    <h1>Blazing Pizzas</h1>
    <ul class="pizza-cards">
        @if (specials != null)
        {
            @foreach (var special in specials)
            {
                <li style="background-image: url('@special.ImageUrl')">
                    <div class="pizza-info">
                        <span class="title">@special.Name</span>
                        @special.Description
                        <span class="price">@special.GetFormattedBasePrice()</span>
                    </div>
                </li>
            }
        }
    </ul>
</div>


@code
{
    // List of special offers
    List<PizzaSpecial> specials = new();

    protected override async Task OnInitializedAsync()
    {
        specials = await HttpClient.GetFromJsonAsync<List<PizzaSpecial>>(NavigationManager.BaseUri + "specials");
    }
}

I've tried installing a newer version of System.Net.Http.Json but the error persisted.

I'm following the Microsoft tutorial on Blazor. I'm getting the 'HttpClient doesn't contain a definition for 'GetFromJsonAsync' error when trying to use this extension method in the Index.razor page. System.Net.Http.Json (v. 6.0.0) is installed.

This is what my Index.razor file looks like:

@page "/"

@using System.Net.Http.Json

<div class="main">
    <h1>Blazing Pizzas</h1>
    <ul class="pizza-cards">
        @if (specials != null)
        {
            @foreach (var special in specials)
            {
                <li style="background-image: url('@special.ImageUrl')">
                    <div class="pizza-info">
                        <span class="title">@special.Name</span>
                        @special.Description
                        <span class="price">@special.GetFormattedBasePrice()</span>
                    </div>
                </li>
            }
        }
    </ul>
</div>


@code
{
    // List of special offers
    List<PizzaSpecial> specials = new();

    protected override async Task OnInitializedAsync()
    {
        specials = await HttpClient.GetFromJsonAsync<List<PizzaSpecial>>(NavigationManager.BaseUri + "specials");
    }
}

I've tried installing a newer version of System.Net.Http.Json but the error persisted.

Share Improve this question edited Feb 6 at 6:09 Tiny Wang 16k2 gold badges18 silver badges38 bronze badges asked Feb 5 at 17:17 MartinMartin 1 4
  • 3 Step 3 states what you need to do to fix the errors – Paweł Łukasik Commented Feb 5 at 17:26
  • In the tutorial, you can search for Http and you will see it requires you to register http service in Program.cs via builder.Services.AddHttpClient(); and you also need to inject this service into your razor component by @inject HttpClient HttpClient to resolve compilation error. By the way, for test purpose you'd better not to use a newer version to avoid version conflict. – Tiny Wang Commented Feb 6 at 6:10
  • Thanks, I should read the tutorials more carefully. – Martin Commented Feb 6 at 9:04
  • .NET 6 reached End Of Life last year. The oldest supported version is .NET 8. There's no reason to target .NET 6 in a new project – Panagiotis Kanavos Commented Feb 6 at 9:09
Add a comment  | 

1 Answer 1

Reset to default 0

Your httpclient isn't directly available in the index.razor component, so you can inject it manually

Update your .razor file

@inject HttpClient Http
@inject NavigationManager NavigationManager

Update your OnInitializedAsync to use:

await Http.GetFromJsonAsync
发布评论

评论列表(0)

  1. 暂无评论