dotnet/aspnetcore · error · ArgumentException

The URI '{uri}' is not contained by the base URI '{baseUri}'

Error message

The URI '{uri}' is not contained by the base URI '{baseUri}'.

What it means

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.

Source

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

            length = baseUri.OriginalString.Length - 1;
            return true;
        }

        length = 0;
        return false;
    }

    private static void Validate(Uri? baseUri, string uri)
    {
        if (baseUri == null || uri == null)
        {
            return;
        }

        if (!TryGetLengthOfBaseUriPrefix(baseUri, uri, out _))
        {
            var message = $"The URI '{uri}' is not contained by the base URI '{baseUri}'.";
            throw new ArgumentException(message);
        }
    }

    private sealed class LocationChangingRegistration : IDisposable
    {
        private readonly Func<LocationChangingContext, ValueTask> _handler;
        private readonly NavigationManager _navigationManager;

        public LocationChangingRegistration(Func<LocationChangingContext, ValueTask> handler, NavigationManager navigationManager)
        {
            _handler = handler;
            _navigationManager = navigationManager;
        }

        public void Dispose()
        {
            _navigationManager.RemoveLocationChangingHandler(_handler);
        }

View on GitHub (pinned to 294cab2f9b)

Solutions

  1. Ensure the URI passed to Initialize/setter shares the BaseUri scheme, host, and path prefix.
  2. Fix BaseUri initialization to reflect the real deployed root (NormalizeBaseUri adds the trailing slash).
  3. Normalize/relativize external URIs before assigning them to Uri.

Example fix

// before (BaseUri = http://app/sub/)
navManager.Initialize("http://app/sub/", "http://other-host/page");

// after
navManager.Initialize("http://app/sub/", "http://app/sub/page");
Defensive patterns

Strategy: validation

Validate before calling

static void ValidateUriAgainstBase(Uri baseUri, string uri)
{
    if (uri is null) return;
    if (!uri.StartsWith(baseUri.OriginalString, StringComparison.Ordinal) &&
        !(baseUri.OriginalString.EndsWith('/') &&
          (uri.IndexOfAny(new[] {'#','?'}) is var i && (i < 0 ? uri : uri[..i]) == baseUri.OriginalString[..^1])))
        throw new ArgumentException($"{uri} not under {baseUri}");
}

Type guard

static bool UriMatchesBase(Uri baseUri, string uri) =>
    uri.StartsWith(baseUri.OriginalString, StringComparison.Ordinal) ||
    (baseUri.OriginalString.EndsWith('/') && uri.Split('#','?')[0] == baseUri.OriginalString[..^1]);

Try / catch

try { navManager.Initialize(baseUri, uri); }
catch (ArgumentException ex) when (ex.Message.Contains("not contained by the base URI"))
{ logger.LogError(ex, "URI/base mismatch on Initialize"); throw; }

Prevention

When it happens

Trigger: 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.

Common situations: Reverse proxy or load balancer returning a different host than BaseUri; misconfigured base href; programmatic Uri assignment from external URLs without relativizing.

Related errors


AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06). Data as JSON: /api/errors/8d68b1c26a52577e. Report an issue: GitHub.