C# InvalidOperationException:无法为属性'_客户工厂';关于类型';coronappcsarp.Pages.Index';

C# InvalidOperationException:无法为属性'_客户工厂';关于类型';coronappcsarp.Pages.Index';,c#,api,asp.net-core,blazor,blazor-server-side,C#,Api,Asp.net Core,Blazor,Blazor Server Side,我正在使用以下api创建此Corona应用程序:。我已经为卡片创建了一个组件,用于显示api中的数据,当我运行应用程序时,它会出现以下错误: 我使用的是System.Net.Http.Json 这是我的模型: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace CoronaAppCsarp.Data { public

我正在使用以下api创建此Corona应用程序:。我已经为卡片创建了一个组件,用于显示api中的数据,当我运行应用程序时,它会出现以下错误:

我使用的是System.Net.Http.Json

这是我的模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CoronaAppCsarp.Data
{
        public class CoronaModel
        {
            public Confirmed confirmed { get; set; }
            public Recovered recovered { get; set; }
            public Deaths deaths { get; set; }
            public string dailySummary { get; set; }
            public string image { get; set; }
            public DateTime lastUpdate { get; set; }
        }

        public class Confirmed
        {
            public int value { get; set; }
            public string detail { get; set; }
        }

        public class Recovered
        {
            public int value { get; set; }
            public string detail { get; set; }
        }

        public class Deaths
        {
            public int value { get; set; }
            public string detail { get; set; }
        }
    }
我的卡组件:

<div class="card" style="width: 18rem;">
    <div class="card-body">
        <h5 class="card-title">@Title</h5>
        <p class="card-text">@NumberOfCases</p>
        <p class="card-text">@Date</p>
        <p class="card-text">@Description.</p>
    </div>
</div>

@code {
    [Parameter]
    public string Title { get; set; }

    [Parameter]
    public float NumberOfCases { get; set; }

    [Parameter]
    public DateTime Date { get; set; }

    [Parameter]
    public string Description { get; set; }
}

@头衔

@NumberOfCases

@日期

@说明

@代码{ [参数] 公共字符串标题{get;set;} [参数] 公共浮点数例{get;set;} [参数] 公共日期时间日期{get;set;} [参数] 公共字符串说明{get;set;} }
和我的索引页:

@page "/"
@inject IHttpClientFactory _clientFactory;

@if (string.IsNullOrWhiteSpace(errorString) == false)
{
    <div class="h2">@errorString</div>
}
else if (coronaModel is null)
{
    <div class="h2">Loading...</div>
}
else
{
    <div class="container-fluid">
        <div class="row">
            <div class="col">
                <Cards NumberOfCases="@coronaModel.confirmed.value" Date="@coronaModel.lastUpdate" Description="Number of active COVID-19 cases" Title="Infected" />
            </div>
            <div class="col">
                <Cards NumberOfCases="@coronaModel.recovered.value" Date="@coronaModel.lastUpdate" Description="Number recovered COVID-19 cases" Title="Recovered" />
            </div>
            <div class="col">
                <Cards NumberOfCases="@coronaModel.deaths.value" Date="@coronaModel.lastUpdate" Description="Number of COVID-19 deaths" Title="Deaths" />
            </div>
        </div>
    </div>
}

@code{
    CoronaModel coronaModel;
    string errorString;

    protected override async Task OnInitializedAsync()
    {
        var request = new HttpRequestMessage(HttpMethod.Get, "https://covid19.mathdro.id/api");

        var client = _clientFactory.CreateClient();

        HttpResponseMessage response = await client.SendAsync(request);

        if (response.IsSuccessStatusCode)
        {
            coronaModel = await response.Content.ReadFromJsonAsync<CoronaModel>();
            errorString = null;
        }
        else
        {
            errorString = "There was an error getting the data.";
        }
    }

}
@page/“
@注入IHttpClientFactory\u客户工厂;
@if(string.IsNullOrWhiteSpace(errorString)==false)
{
@错误字符串
}
else if(模型为空)
{
加载。。。
}
其他的
{
}
@代码{
冠状模型;
字符串错误字符串;
受保护的重写异步任务OnInitializedAsync()
{
var request=newhttprequestmessage(HttpMethod.Get)https://covid19.mathdro.id/api");
var client=_clientFactory.CreateClient();
HttpResponseMessage response=等待客户端.SendAsync(请求);
if(响应。IsSuccessStatusCode)
{
coronaModel=wait response.Content.ReadFromJsonAsync();
errorString=null;
}
其他的
{
errorString=“获取数据时出错。”;
}
}
}
有办法解决吗?如果是的话,我将非常感谢你的帮助


谢谢大家!

您应该将
IHttpClientFactory
服务添加到startup类中的DI容器中
IHttpClientFactory
可以通过如下方式调用AddHttpClient进行注册:

public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpClient();
    //..................
}

这样一个新手的错误,我感到很尴尬。这解决了问题,谢谢!