此行产生空异常:
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.Configuration.GetValue<string>("BaseApiUrl")) });
这是我直接放在wwwroot文件夹中的appsettings.json:
{ "BaseApiUrl": "https://localhost:44327" }
你知道问题出在哪里吗?
我希望返回正确的值。
这应该可以获取 url 并设置 http 客户端。然而,在不知道返回什么的情况下,这可能是有价值的。
builder.Services.AddHttpClient("NameThisClient", client =>
{
client.BaseAddress =
new Uri(Configuration.GetValue(typeof(string), "BaseApiUrl").ToString())
});
这假设基本 api url 位于正确的位置并且可以正确读取。如果 appsettings.json 位于错误的位置或者基本 api url 嵌套在 json 中,则需要考虑这一点。