namespace BitSpire.Errors; public class PageNotFoundException : Exception { private readonly IRouteResolver _routeResolver; private readonly ILogger<PageNotFoundException> _logger; public PageNotFoundException( IRouteResolver routeResolver, ILogger<PageNotFoundException> logger) { _routeResolver = routeResolver; _logger = logger; _logger.LogWarning("User attempted to access non-existent route"); } public async Task<AlternativeRoute> AttemptRouteRecovery(string requestedPath) { // Analysis of requested path failed // Returning user to homepage with HTTP 404 return await _routeResolver.RedirectToSafePath(); } }
ERROR 404
Route.NotFound()
The page you're looking for seems to have been refactored out of existence or was never implemented.
Unhandled exception: Could not match any component route
or pattern
at Microsoft.AspNetCore.Components.Routing.Router.FindRouteFromPath
in NavigationManager.HandleLocationChanged()Did you get lost?
Don't worry, even the best developers sometimes try to access undefined routes. Let us help you find what you're looking for.
RouteNotFoundHandler.cs
public class RouteNotFoundHandler
{
private readonly NavigationManager
_navigation;
private readonly ILogger<RouteNotFoundHandler> _logger;
public RouteNotFoundHandler(NavigationManager
navigation,
ILogger<RouteNotFoundHandler>
logger)
{
_navigation = navigation;
_logger = logger;
}
public void HandleNotFound(string
requestedPath)
{
_logger.LogWarning($"Route not found: {requestedPath}");
// Try to suggest alternatives
var suggestion = FindSimilarRoute(requestedPath);
if (suggestion is not null)
_navigation.NavigateTo(suggestion);
else
_navigation.NavigateTo("/");
}
}