diff --git a/src/ChatAgent.Client/Program.cs b/src/ChatAgent.Client/Program.cs index 2d60c4e..83ce5ba 100644 --- a/src/ChatAgent.Client/Program.cs +++ b/src/ChatAgent.Client/Program.cs @@ -26,12 +26,17 @@ builder.RootComponents.Add("#app"); // content is appended after existing head elements. builder.RootComponents.Add("head::after"); -// Read the API base URL from configuration. -// In Blazor WASM, configuration comes from wwwroot/appsettings.json. +// Determine the API base URL by matching the protocol the app was loaded with. +// In Blazor WASM, builder.HostEnvironment.BaseAddress is the URL the browser +// used to load the app (e.g., "http://localhost:5100/" or "https://localhost:5200/"). +// We check if the app was loaded over HTTPS and pick the corresponding API URL. // IMPORTANT: wwwroot/ files are PUBLIC -- they are downloaded to the browser. // Never put secrets (API keys, passwords) in appsettings.json for a WASM app. // The API key lives server-side in the ChatAgent.Api project. -var apiBaseUrl = builder.Configuration["ApiBaseUrl"] ?? "https://localhost:7100"; +var isHttps = builder.HostEnvironment.BaseAddress.StartsWith("https://", StringComparison.OrdinalIgnoreCase); +var apiBaseUrl = isHttps + ? builder.Configuration["ApiBaseUrl_Https"] ?? "https://localhost:7100" + : builder.Configuration["ApiBaseUrl_Http"] ?? "http://localhost:7000"; // AddHttpClient registers a typed HttpClient using IHttpClientFactory. // IHttpClientFactory manages the underlying HttpMessageHandler lifetime to prevent diff --git a/src/ChatAgent.Client/wwwroot/appsettings.json b/src/ChatAgent.Client/wwwroot/appsettings.json index c542028..64ceea0 100644 --- a/src/ChatAgent.Client/wwwroot/appsettings.json +++ b/src/ChatAgent.Client/wwwroot/appsettings.json @@ -1,3 +1,4 @@ { - "ApiBaseUrl": "https://localhost:7100" + "ApiBaseUrl_Http": "http://localhost:7000", + "ApiBaseUrl_Https": "https://localhost:7100" }