InvalidOperationException:提供了无效的请求URI。请求URI必须是绝对URI,或者必须设置BaseAddress。(HttpClient HttpClient,字符串requestUri(
public class IndexBase:ComponentBase
{
public readonly HttpClient httpClient = new HttpClient();
public IEnumerable<Category> categories { get; set; }
protected override async Task OnInitializedAsync()
{
categories= await httpClient.GetJsonAsync<Category[]>("api/Category");
}
}
并在启动中添加代码
services.AddHttpClient("ServerApi",clint =>
{
clint.BaseAddress = new Uri("https://localhost:44363/");
});
您的NullReferenceException
似乎是因为httpClient
为空而出现的。
您应该像这样制作httpClient
,以便依赖注入可以正确设置它:
public class IndexBase : ComponentBase
{
[Inject]
public HttpClient httpClient { get; set; }
}
它需要一个属性,而不是字段,并且需要一个Inject属性。