C
C#4mo ago
Kyr

.NET 8 / Blazor Server+WASM / Configuration

In Blazor is there a way to pass configuration (i.e. appsettings / environment variables) from the Blazor Server project to the Blazor WASM project? In the .NET 8 Blazor template it creates two projects - one a Server project and one a WASM project. You just build and publish the Server project - it references the WASM project and that gets published along with it. I'm using .NET Aspire, which can only reference the server project in the AppHost. Aspire is setting environment variables (i.e. Base URL for a separate API) and I need the WASM project to get that to configure the named HttpClient in Program.cs. Any ideas / suggestions on how I can do this? UPDATE: I've really been trying to give Blazor a good shot in this project but I'm getting nowhere, fast and I'm just done with it. It's turned out to be just like every other time I've tried to get to grips with Blazor over the years. - hit a roadblock on something that, in my mind at least, seems like it should be relatively simple. - ask for help in various places, SO, Discord, etc. - Get zero help or support - Go back to NextJS and tell my team not to bother with Blazor still I really want to like Blazor because the concept is so nice. But its just not worth the stress at this point. In the time I've spent today trying to do this one simple thing I could've been half done with my app in NextJS, Vue or virtually any other JS framework. So frustrating.
1 Reply
Kyr
Kyr4mo ago
I think I'm going about this all wrong for Blazor but I don't know the right way to do it. Ultimately I have a .NET Aspire AppHost that is passing an environment variable to Blazor... simplified example below.
// Aspire AppHost Program.cs

var api = builder.AddProject<Projects.Application_Host_Api>("TemplateProject-Api");
builder.AddProject<Projects.Presentation_Blazor>("TemplateProject-Presentation-Blazor")
.WithReference(api)
.WithEnvironment("API_BASE_URL", api.Resource.GetEndpoint("https"))
// Aspire AppHost Program.cs

var api = builder.AddProject<Projects.Application_Host_Api>("TemplateProject-Api");
builder.AddProject<Projects.Presentation_Blazor>("TemplateProject-Presentation-Blazor")
.WithReference(api)
.WithEnvironment("API_BASE_URL", api.Resource.GetEndpoint("https"))
// Presentation.Blazor.Client.Program.cs (the WASM bit)

var apiBaseUrl = builder.Configuration.GetValue<string>("API_BASE_URL"); // This is always null
builder.Services.AddHttpClient("API", client => { client.BaseAddress = new Uri(apiBaseUrl); })
.AddHttpMessageHandler<AuthenticatedHttpClientHandler>();
// Presentation.Blazor.Client.Program.cs (the WASM bit)

var apiBaseUrl = builder.Configuration.GetValue<string>("API_BASE_URL"); // This is always null
builder.Services.AddHttpClient("API", client => { client.BaseAddress = new Uri(apiBaseUrl); })
.AddHttpMessageHandler<AuthenticatedHttpClientHandler>();
How do I get API_BASE_URL in the WASM client like this? Ignore this - I'm done with blazor!
Want results from more Discord servers?
Add your server