fix(01-02): auto-match API URL protocol to client load protocol
Client now detects whether it was loaded over HTTP or HTTPS and selects the corresponding API base URL, so both protocols work without manual config changes. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -26,12 +26,17 @@ builder.RootComponents.Add<App>("#app");
|
||||
// content is appended after existing head elements.
|
||||
builder.RootComponents.Add<HeadOutlet>("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<ChatApiClient> registers a typed HttpClient using IHttpClientFactory.
|
||||
// IHttpClientFactory manages the underlying HttpMessageHandler lifetime to prevent
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
{
|
||||
"ApiBaseUrl": "https://localhost:7100"
|
||||
"ApiBaseUrl_Http": "http://localhost:7000",
|
||||
"ApiBaseUrl_Https": "https://localhost:7100"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user