{"record":{"id":"6603fab0d10f78bd","repo":"PrismLibrary/Prism","slug":"navigationexception-gobackrequiresnavigationpage","errorCode":"NavigationException.GoBackRequiresNavigationPage","errorMessage":"NavigationException.GoBackRequiresNavigationPage","messagePattern":"NavigationException\\.GoBackRequiresNavigationPage","errorType":"exception","errorClass":"NavigationException","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/Navigation/PageNavigationService.cs","lineNumber":173,"sourceCode":"    {\n        await WaitForPendingNavigationRequests();\n        try\n        {\n            parameters ??= new NavigationParameters();\n\n            parameters.GetNavigationParametersInternal().Add(KnownInternalParameters.NavigationMode, NavigationMode.Back);\n\n            var page = GetCurrentPage();\n            var canNavigate = await MvvmHelpers.CanNavigateAsync(page, parameters);\n            if (!canNavigate)\n            {\n                throw new NavigationException(NavigationException.IConfirmNavigationReturnedFalse, page);\n            }\n\n            var pagesToDestroy = page.Navigation.NavigationStack.ToList(); // get all pages to destroy\n            pagesToDestroy.Reverse(); // destroy them in reverse order\n            var goBackPage = pagesToDestroy.FirstOrDefault(p => ViewModelLocator.GetNavigationName(p) == viewName) \n                ?? throw new NavigationException(NavigationException.GoBackRequiresNavigationPage); // find the go back page\n            var index = pagesToDestroy.IndexOf(goBackPage);\n            pagesToDestroy.RemoveRange(index, pagesToDestroy.Count - index); // don't destroy pages from the go back page to the root page\n            var pagesToRemove = pagesToDestroy.Skip(1).ToList(); // exclude the current page from the destroy pages\n\n            bool animated = !parameters.ContainsKey(KnownNavigationParameters.Animated) || parameters.GetValue<bool>(KnownNavigationParameters.Animated);\n            NavigationSource = PageNavigationSource.NavigationService;\n            foreach(var removePage in pagesToRemove)\n            {\n                page.Navigation.RemovePage(removePage);\n            }\n            await page.Navigation.PopAsync(animated);\n            NavigationSource = PageNavigationSource.Device;\n\n            foreach (var destroyPage in pagesToDestroy)\n            {\n                MvvmHelpers.OnNavigatedFrom(destroyPage, parameters);\n                MvvmHelpers.DestroyPage(destroyPage);\n            }","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/Navigation/PageNavigationService.cs#L155-L191","documentation":"GoBackToAsync searches the current NavigationStack (in reverse) for a page whose navigation name matches the requested viewName. If no page with that name exists in the stack, goBackPage is null and Prism throws NavigationException with GoBackRequiresNavigationPage — you cannot 'go back to' a page that was never pushed.","triggerScenarios":"Calling GoBackToAsync(\"SomeView\") when SomeView is not anywhere in the current INavigation NavigationStack (never pushed, already popped, or pushed with a different registered name).","commonSituations":"Typos or casing mismatches between the view name registered in Prism and the string passed to GoBackToAsync; assuming a page is on the stack when a previous GoBackToRootAsync/GoBackAsync already removed it; navigating to the target via Uri path syntax so it was created as part of a different name.","solutions":["Verify the page was pushed with exactly the registered name (ViewModelLocator.GetNavigationName) and that it is still on page.Navigation.NavigationStack.","Log/dump the current stack names (ViewModelLocator.GetNavigationName per page) before calling GoBackToAsync to confirm the target exists.","Use GoBackToRootAsync or NavigateAsync with a relative URI instead when the target may not be on the stack.","Fix the view name string to match the registration (e.g. \"Views/MainPage\" vs \"MainPage\")."],"exampleFix":"// before\nawait _navigationService.GoBackToAsync(\"MainPage\");\n// after\nvar stack = _pageAccessor.Page.Navigation.NavigationStack;\nbool exists = stack.Any(p => ViewModelLocator.GetNavigationName(p) == \"MainPage\");\nif (exists)\n    await _navigationService.GoBackToAsync(\"MainPage\");\nelse\n    await _navigationService.NavigateAsync(\"MainPage\");","handlingStrategy":"validation","validationCode":"var stack = currentPage.Navigation.NavigationStack;\nbool targetOnStack = stack.Any(p => ViewModelLocator.GetNavigationName(p) == viewName);\nif (!targetOnStack) await _navigationService.NavigateAsync(viewName);","typeGuard":"bool IsOnStack(Page current, string viewName) =>\n    current.Navigation.NavigationStack.Any(p => ViewModelLocator.GetNavigationName(p) == viewName);","tryCatchPattern":"try { await _navigationService.GoBackToAsync(viewName); }\ncatch (NavigationException ex) when (ex.Message.Contains(NavigationException.GoBackRequiresNavigationPage))\n{ await _navigationService.NavigateAsync(viewName); }","preventionTips":["Confirm the target page is currently on the NavigationStack before GoBackToAsync","Use exact registered view names (watch casing and 'Views/' prefixes)","Prefer NavigateAsync for targets that may not be on the stack"],"tags":["navigation","page-not-found","prism","xamarin-maui"],"backgroundTag":"resource-not-found","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}