aspnetcore: Blazor: Navigation.BaseUri Incorrect on Forwarded Ports
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
Environment: GitHub Codespaces launchSettings URL: http://localhost:5000
NavigationManager.BaseUri does not properly resolve when running in an environment with port forwarding (to either the initial port or the forwarded port). It will initially be set to http://localhost/ before flashing to the forwarded port. This prevents the use of NavigationManager.BaseUri for composing API calls in OnInitializedAsync.
Initial Component Render

Moments After Render

Expected Behavior
NavigationManager.BaseUri should immediately reflect the forwarded port URL so that API calls can be reliably constructed.
Steps To Reproduce
Run the following repo in a GitHub Codespace:
https://github.com/JaimeStill/BlazorPortForwardedBaseUriIssue
See Index.razor.
Exceptions (if any)
No response
.NET Version
6.0.402
Anything else?
.NET SDK (reflecting any global.json): Version: 6.0.402 Commit: 6862418796
Runtime Environment: OS Name: ubuntu OS Version: 20.04 OS Platform: Linux RID: ubuntu.20.04-x64 Base Path: /usr/local/dotnet/6/sdk/6.0.402/
global.json file: Not found
Host: Version: 6.0.10 Architecture: x64 Commit: 5a400c212a
.NET SDKs installed: 6.0.402 [/usr/local/dotnet/6/sdk]
.NET runtimes installed: Microsoft.AspNetCore.App 6.0.10 [/usr/local/dotnet/6/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 6.0.10 [/usr/local/dotnet/6/shared/Microsoft.NETCore.App]
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 17 (12 by maintainers)
@JaimeStill thanks for contacting us.
That is because
https://jaimestill-literate-space-waffle-v6qgjjj56rvcw596-5000.preview.app.github.devis only accessible “from outside”. (meaning your browser).Codespaces apparently has not setup
jaimestill-literate-space-waffle-v6qgjjj56rvcw596-5000.preview.app.github.devto redirect to localhost, which is what should happen.This seems to be something specific to how codespaces sets up the environment and port forwarding. NavigationManager is initialized with HttpContext.Request values during prerendering and it is initialized with the value from the document.baseUri and location.href when it becomes interactive, so that will explain the flashing.
The fact that it does not happen when the app is not prerendered is also strong evidence of that, tied to the fact that you can only reach yourself from within the app via localhost.