PrismLibrary/Prism · error · InvalidOperationException

The constructed URI contains one or more relative back…

Error message

The constructed URI contains one or more relative back operators, but contains no other navigation segments - '{uri}'.

What it means

BuildUri validates relative URIs containing '..' segments: if every segment is a back operator and there is nothing to navigate back to a sibling of, the constructed URI is meaningless. Prism throws InvalidOperationException including the offending URI so the developer can see what was built.

Solutions

  1. Ensure at least one non-back segment exists: start with a page segment before/after RelativeBack(), e.g. RelativeBack().AddSegment("OtherPage").
  2. Count available navigation stack depth before emitting back operators and cap the number of '..' segments.
  3. If the goal is simply going back in the stack, use INavigationService.GoBackAsync() instead of building a '..'-only URI.

Example fix

// before
navigationService.CreateBuilder().RelativeBack().RelativeBack().NavigateAsync(); // only back segments
// after
navigationService.CreateBuilder().RelativeBack().AddSegment("DetailPage").NavigateAsync();
Defensive patterns

Strategy: validation

Validate before calling

var segs = new[] { "..", ".." }; // whatever you built
if (segs.All(s => s == ".."))
    throw new InvalidOperationException("A relative-back URI must include at least one real page segment; use GoBackAsync() instead.");

Try / catch

try
{
    await navigationService.NavigateAsync(uri);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("no other navigation segments"))
{
    await navigationService.GoBackAsync();
}

Prevention

When it happens

Trigger: Chaining only RelativeBack() calls (or AddSegment("..") repeatedly) with no real page segments, then calling NavigateAsync() or Uri — e.g. builder.RelativeBack().RelativeBack().NavigateAsync().

Common situations: Over-estimating how many back steps are available; building the URI programmatically in a loop that emits only '..' segments; calling RelativeBack on a fresh builder with no preceding page segment.

Understand the failure class

Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/102404ee7185d0d4. Report an issue: GitHub.

Appendix: source

Thrown at src/Maui/Prism.Maui/Navigation/Builder/NavigationBuilder.cs:113

        foreach ((var key, var value) in parameters)
            _navigationParameters.Add(key, value);

        return this;
    }

    internal Uri BuildUri()
    {
        var uri = (_absoluteNavigation ? "/" : string.Empty) +
            string.Join("/", _uriSegments.Select(x => x.Segment));

        if(uri.Contains("../"))
        {
            if (_absoluteNavigation)
                throw new InvalidOperationException("The generated URI has one or more relative back operators and was marked as an absolute path. This is not supported.");

            var segments = uri.Split('/');
            if (!segments.Any(x => x != ".."))
                throw new InvalidOperationException($"The constructed URI contains one or more relative back operators, but contains no other navigation segments - '{uri}'.");
            var hasNonBackSegment = false;
            for(int i = 0; i < segments.Length; i++)
            {
                if (hasNonBackSegment && segments[i] == "..")
                    throw new InvalidOperationException($"The constructed URI has a relative back operator after a new Navigation Segment which is not supported - '{uri}'.");
                else if (segments[i] != "..")
                    hasNonBackSegment = true;
            }
        }

        return UriParsingHelper.Parse(uri);
    }
}

View on GitHub (pinned to 358118cd64)