{"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/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Components/Components/src/NavigationManager.cs#L635-L671","documentation":"The static Validate method throws ArgumentException when setting Uri and the new URI is not contained by the base URI (prefix match fails, including the trailing-slash special case). This guards the Uri setter so an inconsistent base/uri pair cannot be stored.","triggerScenarios":"Calling Initialize(baseUri, uri) or setting Uri with a value that does not share the base URI prefix; base URI configured without trailing slash normalization producing mismatches; cross-origin absolute URI assigned to Uri.","commonSituations":"Reverse proxy or load balancer returning a different host than BaseUri; misconfigured base href; programmatic Uri assignment from external URLs without relativizing.","solutions":["Ensure the URI passed to Initialize/setter shares the BaseUri scheme, host, and path prefix.","Fix BaseUri initialization to reflect the real deployed root (NormalizeBaseUri adds the trailing slash).","Normalize/relativize external URIs before assigning them to Uri."],"exampleFix":"// before (BaseUri = http://app/sub/)\nnavManager.Initialize(\"http://app/sub/\", \"http://other-host/page\");\n\n// after\nnavManager.Initialize(\"http://app/sub/\", \"http://app/sub/page\");","handlingStrategy":"validation","validationCode":"static void ValidateUriAgainstBase(Uri baseUri, string uri)\n{\n    if (uri is null) return;\n    if (!uri.StartsWith(baseUri.OriginalString, StringComparison.Ordinal) &&\n        !(baseUri.OriginalString.EndsWith('/') &&\n          (uri.IndexOfAny(new[] {'#','?'}) is var i && (i < 0 ? uri : uri[..i]) == baseUri.OriginalString[..^1])))\n        throw new ArgumentException($\"{uri} not under {baseUri}\");\n}","typeGuard":"static bool UriMatchesBase(Uri baseUri, string uri) =>\n    uri.StartsWith(baseUri.OriginalString, StringComparison.Ordinal) ||\n    (baseUri.OriginalString.EndsWith('/') && uri.Split('#','?')[0] == baseUri.OriginalString[..^1]);","tryCatchPattern":"try { navManager.Initialize(baseUri, uri); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"not contained by the base URI\"))\n{ logger.LogError(ex, \"URI/base mismatch on Initialize\"); throw; }","preventionTips":["Keep BaseUri and Uri from the same source (request or config).","Normalize base URIs to include a trailing slash.","Validate external URIs before assigning to Uri."],"tags":["blazor","navigation","uri","base-path","validation"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}