{"record":{"id":"88713b1b4d5a4a01","repo":"dotnet/aspnetcore","slug":"an-exception-occurred-while-dispatching-a-location","errorCode":null,"errorMessage":"An exception occurred while dispatching a location changed event.","messagePattern":"An exception occurred while dispatching a location changed event\\.","errorType":"exception","errorClass":"LocationChangeException","httpStatus":null,"severity":"error","filePath":"src/Components/Components/src/NavigationManager.cs","lineNumber":403,"sourceCode":"    }\n\n    /// <summary>\n    /// Triggers the <see cref=\"LocationChanged\"/> event with the current URI value.\n    /// </summary>\n    protected void NotifyLocationChanged(bool isInterceptedLink)\n    {\n        try\n        {\n            _locationChanged?.Invoke(\n                this,\n                new LocationChangedEventArgs(_uri!, isInterceptedLink)\n                {\n                    HistoryEntryState = HistoryEntryState\n                });\n        }\n        catch (Exception ex)\n        {\n            throw new LocationChangeException(\"An exception occurred while dispatching a location changed event.\", ex);\n        }\n    }\n\n    /// <summary>\n    /// Notifies the registered handlers of the current location change.\n    /// </summary>\n    /// <param name=\"uri\">The destination URI. This can be absolute, or relative to the base URI.</param>\n    /// <param name=\"state\">The state associated with the target history entry.</param>\n    /// <param name=\"isNavigationIntercepted\">Whether this navigation was intercepted from a link.</param>\n    /// <returns>A <see cref=\"ValueTask{TResult}\"/> representing the completion of the operation. If the result is <see langword=\"true\"/>, the navigation should continue.</returns>\n    protected async ValueTask<bool> NotifyLocationChangingAsync(string uri, string? state, bool isNavigationIntercepted)\n    {\n        _locationChangingCts?.Cancel();\n        _locationChangingCts = null;\n\n        var handlerCount = _locationChangingHandlers.Count;\n\n        if (handlerCount == 0)","sourceCodeStart":385,"sourceCodeEnd":421,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Components/Components/src/NavigationManager.cs#L385-L421","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Wrap your LocationChanged handler body in try/catch to keep handler failures local.","Inspect the InnerException of the thrown LocationChangeException to find the real cause.","Avoid heavy/synchronous work in LocationChanged handlers."],"exampleFix":"// before\nnavManager.LocationChanged += (s, e) => DoAnalytics(e.Location);\n\n// after\nnavManager.LocationChanged += (s, e) =>\n{\n    try { DoAnalytics(e.Location); }\n    catch (Exception ex) { logger.LogError(ex, \"analytics failed\"); }\n};","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"navManager.LocationChanged += (s, e) =>\n{\n    try { OnLocationChanged(e); }\n    catch (Exception ex) { logger.LogError(ex, \"LocationChanged handler failed\"); }\n};\n\n// at the navigation call site:\ntry { navManager.NavigateTo(uri); }\ncatch (LocationChangeException ex)\n{ logger.LogError(ex.InnerException, \"A subscriber threw during navigation\"); }","preventionTips":["Always wrap LocationChanged handler bodies in try/catch.","Inspect InnerException of LocationChangeException for the real cause.","Keep handlers lightweight and side-effect free."],"tags":["blazor","navigation","events","exception-handling"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}