Peter Morris
Peter Morris

Reputation: 23224

Get server URL in Program.Main of a Blazor WebAssembly app

I want Program.Main in my Blazor WebAssembly app to make a call to the server that served the app before booting up the app - so I can grab some configuration settings from the server.

Is there a way to determine what the server URL was?

Upvotes: 1

Views: 501

Answers (1)

Peter Morris
Peter Morris

Reputation: 23224

Looks like I can make a server request in Program.Main like this

private static Task<ClientConfiguration> GetClientConfigurationAsync()
{
    var serviceProvider = new ServiceCollection()
        .AddBaseAddressHttpClient()
        .BuildServiceProvider();

    var httpClient = serviceProvider.GetRequiredService<HttpClient>();
    return httpClient.GetJsonAsync<ClientConfiguration>("api/client-configuration");
}

Upvotes: 2

Related Questions