我正在尝试在Blazor WASM的Program.cs类中配置多个API URL。我没有在服务器端看到AddHttpClient扩展。想知道是否有人对此有替代解决方案?
这是我到目前为止所拥有的:
CREATE VIEW v_location_averages AS
SELECT
r.location_id,
t.name,
t.eating,
t.reading,
t.child_friendly,
t.pet_friendly,
t.accessible,
(COALESCE(t.eating, 0) + COALESCE(t.reading, 0) + COALESCE(t.child_friendly, 0) + COALESCE(t.pet_friendly, 0) + COALESCE(t.accessible, 0)) / num_nonnulls(t.eating, t.reading, t.child_friendly, t.pet_friendly, t.accessible) AS "overall_rating"
FROM (
SELECT
locations.name,
location_id,
AVG(eating) AS "eating",
AVG(reading) AS "reading",
AVG(child_friendly) AS "child_friendly",
AVG(pet_friendly) AS "pet_friendly",
AVG(accessible) AS "accessible",
FROM ratings
INNER JOIN locations on ratings.location_id = locations.id
GROUP BY location_id, locations.name
) t
JOIN ratings r ON r.location_id = t.location_id
GROUP BY r.location_id, t.name, t.eating, t.reading, t.child_friendly, t.pet_friendly, t.accessible;
答案 0 :(得分:7)
这可以通过Blazor客户端来完成。首先,在您的客户端程序包中,获取以下nuget程序包: Microsoft.Extensions.Http
然后,为该示例创建两个类(通常,您将使用接口,但是单独的类应在此处工作。我将演示正在使用的两个不同的基地址,因此您知道有区别。
> public class GoogleService
{
private readonly HttpClient httpClient;
public GoogleService(HttpClient httpClient)
{
this.httpClient = httpClient;
}
public string GetBaseUrl()
{
return httpClient.BaseAddress.ToString();
}
}
和Yahoo服务:
public class YahooService
{
private readonly HttpClient httpClient;
public YahooService(HttpClient httpClient)
{
this.httpClient = httpClient;
}
public string GetBaseUrl()
{
return httpClient.BaseAddress.ToString();
}
}
下一步,在客户程序的Program.cs中,您可以执行以下操作:
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddHttpClient<GoogleService>(client =>
{
client.BaseAddress = new Uri("https://google.com/");
});
builder.Services.AddHttpClient<YahooService>(client =>
{
client.BaseAddress = new Uri("https://yahoo.com/");
});
await builder.Build().RunAsync();
}
接下来,您可以像这样将它们注入您的前端,并查看它们确实是两个不同的注入客户端:
@page "/"
@inject BlazorHttpClientTest.Client.Clients.GoogleService googleService;
@inject BlazorHttpClientTest.Client.Clients.YahooService yahooService;
<h1>Hello, world!</h1>
<label>Google Address:</label><label>@googleAddress</label>
<label>Yahoo Address:</label><label>@yahooAddress</label>
@code{
string googleAddress;
string yahooAddress;
protected override void OnInitialized()
{
base.OnInitialized();
googleAddress = googleService.GetBaseUrl();
yahooAddress = yahooService.GetBaseUrl();
}
}
就这样,您应该使它工作:
让我知道是否需要我进一步解释其他内容,否则,请标记为已回答,如果它对您有用。