dotnet/aspnetcore · error · InvalidOperationException

' ' has not been initialized.

Error message

'{GetType().Name}' has not been initialized.

What it means

Thrown by AssertInitialized when, after invoking EnsureInitialized, the manager is still not initialized. NavigationManager defers first-time setup to EnsureInitialized (overridable); if a subclass does not call Initialize(baseUri, uri) from it (or from its constructor), every public API that needs BaseUri/Uri refuses to proceed.

Solutions

  1. In tests, call Initialize(baseUri, uri) on the manager immediately after construction.
  2. Inject NavigationManager via @inject/[Inject] and use it only from lifecycle methods (OnInitialized/OnInitializedAsync onward), not from constructors or field initializers.
  3. If you subclass NavigationManager, override EnsureInitialized and call Initialize, or call Initialize from the subclass constructor.
  4. For the FakeNavigationManager test helper, use its Initialize method rather than hand-rolling setup.

Example fix

// before: used before the framework initialized it
public class MyComponent : ComponentBase
{
    [Inject] NavigationManager Nav { get; set; } = default!;
    string current = Nav.Uri; // throws in field init
}

// after: read inside a lifecycle hook
public class MyComponent : ComponentBase
{
    [Inject] NavigationManager Nav { get; set; } = default!;
    protected override void OnInitialized() => current = Nav.Uri;
}
Defensive patterns

Strategy: validation

Validate before calling

private static bool IsReady(NavigationManager nm)
{
    try { _ = nm.BaseUri; return true; }
    catch (InvalidOperationException) { return false; }
}

if (!IsReady(navManager)) throw new InvalidOperationException("NavigationManager not initialized; call Initialize first.");

Try / catch

try { var uri = navManager.Uri; }
catch (InvalidOperationException ex) when (ex.Message.Contains("not been initialized"))
{ /* defer the operation until OnInitializedAsync runs */ }

Prevention

When it happens

Trigger: Accessing Uri, BaseUri, ToAbsoluteUri, NavigateTo, the LocationChanged event, RegisterLocationChangingHandler, or ToBaseRelativePath on a NavigationManager whose Initialize was never called. Typical with a hand-constructed instance (tests, a custom host that forgot the init step, or accessing the manager before the framework wired it).

Common situations: Constructing NavigationManager directly in a unit test without calling Initialize; a custom host that resolves the manager from DI before the framework's host service has run its initialization; reading NavigationManager.Uri in a constructor or field initializer of a component before OnInitializedAsync.

Related errors


AI-assisted analysis of dotnet/aspnetcore@3600ca084e (2026-08-11). Data as JSON: /api/errors/aea434e251235b41. Report an issue: GitHub.

Appendix: source

Thrown at src/Components/Components/src/NavigationManager.cs:611

    {
        AssertInitialized();

        if (_locationChangingHandlers.Remove(locationChangingHandler) && _locationChangingHandlers.Count == 0)
        {
            SetNavigationLockState(false);
        }
    }

    private void AssertInitialized()
    {
        if (!_isInitialized)
        {
            EnsureInitialized();
        }

        if (!_isInitialized)
        {
            throw new InvalidOperationException($"'{GetType().Name}' has not been initialized.");
        }
    }

    private static bool TryGetLengthOfBaseUriPrefix(Uri baseUri, string uri, out int length)
    {
        if (uri.StartsWith(baseUri.OriginalString, StringComparison.Ordinal))
        {
            // The absolute URI must be of the form "{baseUri}something" (where
            // baseUri ends with a slash), and from that we return "something"
            length = baseUri.OriginalString.Length;
            return true;
        }

        var pathEndIndex = uri.AsSpan().IndexOfAny('#', '?');
        var uriPathOnly = pathEndIndex < 0 ? uri : uri.AsSpan(0, pathEndIndex);
        if (baseUri.OriginalString.EndsWith('/') && uriPathOnly.Equals(baseUri.OriginalString.AsSpan(0, baseUri.OriginalString.Length - 1), StringComparison.Ordinal))
        {
            // Special case: for the base URI "/something/", if you're at

View on GitHub (pinned to 3600ca084e)