{"record":{"id":"8d68b1c26a52577e","repo":"dotnet/aspnetcore","slug":"the-uri-uri-is-not-contained-by-the-base-uri-8d68b1","errorCode":null,"errorMessage":"The URI '{uri}' is not contained by the base URI '{baseUri}'.","messagePattern":"The URI '(.+?)' is not contained by the base URI '(.+?)'\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Components/Components/src/NavigationManager.cs","lineNumber":653,"sourceCode":"            length = baseUri.OriginalString.Length - 1;\n            return true;\n        }\n\n        length = 0;\n        return false;\n    }\n\n    private static void Validate(Uri? baseUri, string uri)\n    {\n        if (baseUri == null || uri == null)\n        {\n            return;\n        }\n\n        if (!TryGetLengthOfBaseUriPrefix(baseUri, uri, out _))\n        {\n            var message = $\"The URI '{uri}' is not contained by the base URI '{baseUri}'.\";\n            throw new ArgumentException(message);\n        }\n    }\n\n    private sealed class LocationChangingRegistration : IDisposable\n    {\n        private readonly Func<LocationChangingContext, ValueTask> _handler;\n        private readonly NavigationManager _navigationManager;\n\n        public LocationChangingRegistration(Func<LocationChangingContext, ValueTask> handler, NavigationManager navigationManager)\n        {\n            _handler = handler;\n            _navigationManager = navigationManager;\n        }\n\n        public void Dispose()\n        {\n            _navigationManager.RemoveLocationChangingHandler(_handler);\n        }","sourceCodeStart":635,"sourceCodeEnd":671,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/Components/Components/src/NavigationManager.cs#L635-L671","documentation":"Thrown by the private Validate helper invoked from the Uri setter whenever the new absolute URI is not prefixed by the configured base URI. This guards the invariant that NavigationManager.Uri always lives inside BaseUri; setting anything else would corrupt relative-URI resolution for every subsequent call.","triggerScenarios":"A subclass (or framework host code) assigns an absolute URI to the protected Uri setter that lives outside BaseUri - e.g. restoring a deep link after the <base href> changed, a history entry from a previous deployment, or a programmatic NavigateTo whose resolved absolute form crosses the base boundary.","commonSituations":"Redeploying the app under a new subpath while old history entries still point at the previous base; a server-side redirect landing outside the configured base; manipulating NavigationManager.Uri in tests without aligning BaseUri first.","solutions":["Before assigning Uri, confirm it starts with BaseUri.OriginalString; if not, re-Initialize with matching base/uri.","Keep <base href> stable across deployments, or migrate old URLs via redirect to the new base before they reach NavigationManager.","In tests, set BaseUri first, then Uri; use NavigationManager.Initialize(baseUri, uri) to set both atomically.","When restoring history state, validate the stored URI against the current BaseUri and discard entries that no longer match."],"exampleFix":"// before: assigning a Uri that crossed the base after a subpath change\nBaseUri = \"https://site.com/app/\";\nUri = \"https://site.com/old-path/page\"; // throws\n\n// after: re-initialize with a URI inside the base\nInitialize(\"https://site.com/app/\", \"https://site.com/app/page\");","handlingStrategy":"validation","validationCode":"static bool WithinBase(Uri baseUri, string uri) => uri.StartsWith(baseUri.OriginalString, StringComparison.Ordinal);\n\nif (!WithinBase(baseUri, newUri)) throw new InvalidOperationException(\"Refusing to set Uri outside BaseUri.\");","typeGuard":null,"tryCatchPattern":"try { manager.Uri = candidate; }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"not contained by the base URI\"))\n{ /* reject the candidate, log, fall back */ }","preventionTips":["Set BaseUri before Uri; use Initialize(baseUri, uri) to set both atomically.","Validate restored history URIs against the current BaseUri and discard mismatches.","Keep <base href> stable across deployments; redirect old URLs to the new base before they reach NavigationManager."],"tags":["blazor","routing","uri","invariant"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}