{"record":{"id":"aea434e251235b41","repo":"dotnet/aspnetcore","slug":"gettype-name-has-not-been-initialized","errorCode":null,"errorMessage":"'{GetType().Name}' has not been initialized.","messagePattern":"'(.+?)' has not been initialized\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Components/src/NavigationManager.cs","lineNumber":611,"sourceCode":"    {\n        AssertInitialized();\n\n        if (_locationChangingHandlers.Remove(locationChangingHandler) && _locationChangingHandlers.Count == 0)\n        {\n            SetNavigationLockState(false);\n        }\n    }\n\n    private void AssertInitialized()\n    {\n        if (!_isInitialized)\n        {\n            EnsureInitialized();\n        }\n\n        if (!_isInitialized)\n        {\n            throw new InvalidOperationException($\"'{GetType().Name}' has not been initialized.\");\n        }\n    }\n\n    private static bool TryGetLengthOfBaseUriPrefix(Uri baseUri, string uri, out int length)\n    {\n        if (uri.StartsWith(baseUri.OriginalString, StringComparison.Ordinal))\n        {\n            // The absolute URI must be of the form \"{baseUri}something\" (where\n            // baseUri ends with a slash), and from that we return \"something\"\n            length = baseUri.OriginalString.Length;\n            return true;\n        }\n\n        var pathEndIndex = uri.AsSpan().IndexOfAny('#', '?');\n        var uriPathOnly = pathEndIndex < 0 ? uri : uri.AsSpan(0, pathEndIndex);\n        if (baseUri.OriginalString.EndsWith('/') && uriPathOnly.Equals(baseUri.OriginalString.AsSpan(0, baseUri.OriginalString.Length - 1), StringComparison.Ordinal))\n        {\n            // Special case: for the base URI \"/something/\", if you're at","sourceCodeStart":593,"sourceCodeEnd":629,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/Components/Components/src/NavigationManager.cs#L593-L629","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["In tests, call Initialize(baseUri, uri) on the manager immediately after construction.","Inject NavigationManager via @inject/[Inject] and use it only from lifecycle methods (OnInitialized/OnInitializedAsync onward), not from constructors or field initializers.","If you subclass NavigationManager, override EnsureInitialized and call Initialize, or call Initialize from the subclass constructor.","For the FakeNavigationManager test helper, use its Initialize method rather than hand-rolling setup."],"exampleFix":"// before: used before the framework initialized it\npublic class MyComponent : ComponentBase\n{\n    [Inject] NavigationManager Nav { get; set; } = default!;\n    string current = Nav.Uri; // throws in field init\n}\n\n// after: read inside a lifecycle hook\npublic class MyComponent : ComponentBase\n{\n    [Inject] NavigationManager Nav { get; set; } = default!;\n    protected override void OnInitialized() => current = Nav.Uri;\n}","handlingStrategy":"validation","validationCode":"private static bool IsReady(NavigationManager nm)\n{\n    try { _ = nm.BaseUri; return true; }\n    catch (InvalidOperationException) { return false; }\n}\n\nif (!IsReady(navManager)) throw new InvalidOperationException(\"NavigationManager not initialized; call Initialize first.\");","typeGuard":null,"tryCatchPattern":"try { var uri = navManager.Uri; }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"not been initialized\"))\n{ /* defer the operation until OnInitializedAsync runs */ }","preventionTips":["Inject NavigationManager and only touch it from OnInitialized/OnInitializedAsync onward.","In tests, call Initialize(baseUri, uri) immediately after construction.","Subclassing? Override EnsureInitialized to call Initialize, or call it from the constructor.","Never read NavigationManager.Uri from a field initializer or constructor."],"tags":["blazor","lifecycle","initialization","routing"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}