dotnet/aspnetcore · error · LocationChangeException

An exception occurred while dispatching a location changed e

Error message

An exception occurred while dispatching a location changed event.

What it means

NotifyLocationChanged wraps any exception thrown by a LocationChanged event handler in a LocationChangeException. This prevents one faulty subscriber from masking the navigation pipeline; the original exception is preserved as InnerException.

Source

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

    }

    /// <summary>
    /// Triggers the <see cref="LocationChanged"/> event with the current URI value.
    /// </summary>
    protected void NotifyLocationChanged(bool isInterceptedLink)
    {
        try
        {
            _locationChanged?.Invoke(
                this,
                new LocationChangedEventArgs(_uri!, isInterceptedLink)
                {
                    HistoryEntryState = HistoryEntryState
                });
        }
        catch (Exception ex)
        {
            throw new LocationChangeException("An exception occurred while dispatching a location changed event.", ex);
        }
    }

    /// <summary>
    /// Notifies the registered handlers of the current location change.
    /// </summary>
    /// <param name="uri">The destination URI. This can be absolute, or relative to the base URI.</param>
    /// <param name="state">The state associated with the target history entry.</param>
    /// <param name="isNavigationIntercepted">Whether this navigation was intercepted from a link.</param>
    /// <returns>A <see cref="ValueTask{TResult}"/> representing the completion of the operation. If the result is <see langword="true"/>, the navigation should continue.</returns>
    protected async ValueTask<bool> NotifyLocationChangingAsync(string uri, string? state, bool isNavigationIntercepted)
    {
        _locationChangingCts?.Cancel();
        _locationChangingCts = null;

        var handlerCount = _locationChangingHandlers.Count;

        if (handlerCount == 0)

View on GitHub (pinned to 294cab2f9b)

Solutions

  1. Wrap your LocationChanged handler body in try/catch to keep handler failures local.
  2. Inspect the InnerException of the thrown LocationChangeException to find the real cause.
  3. Avoid heavy/synchronous work in LocationChanged handlers.

Example fix

// before
navManager.LocationChanged += (s, e) => DoAnalytics(e.Location);

// after
navManager.LocationChanged += (s, e) =>
{
    try { DoAnalytics(e.Location); }
    catch (Exception ex) { logger.LogError(ex, "analytics failed"); }
};
Defensive patterns

Strategy: try-catch

Try / catch

navManager.LocationChanged += (s, e) =>
{
    try { OnLocationChanged(e); }
    catch (Exception ex) { logger.LogError(ex, "LocationChanged handler failed"); }
};

// at the navigation call site:
try { navManager.NavigateTo(uri); }
catch (LocationChangeException ex)
{ logger.LogError(ex.InnerException, "A subscriber threw during navigation"); }

Prevention

When it happens

Trigger: A LocationChanged subscriber throws (NullReferenceException, InvalidOperationException, etc.) during navigation; accessing services that are not yet available in the handler; handler performing IO that fails synchronously.

Common situations: Logging/analytics handlers that hit null service references; handlers that assume a component is still mounted; exceptions in OnLocationChanged callbacks during fast back-to-back navigations.

Related errors


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