dotnet/aspnetcore · error · InvalidOperationException

No component found for route '{route}'. Ensure the route mat

Error message

No component found for route '{route}'. Ensure the route matches a component with a [Route] attribute.

What it means

Thrown by Blazor's Router when RenderComponentByRoute is asked to render a route that resolves to no component. RenderComponentByRoute calls FindComponentTypeByRoute, which normalizes the route, runs it through the compiled RouteTable, and returns null if no @page/[Route] handler matches. This InvalidOperationException surfaces during the NotFound/redirect flow when a subscriber sets a path that the router cannot resolve. It indicates a mismatch between the URL being navigated and the set of routable components discovered at startup.

Source

Thrown at src/Components/Components/src/Routing/Router.cs:416

                _renderHandle.Render(builder => RenderComponentByRoute(builder, args.Path));
            }
            else
            {
                // Having the path set signals to the endpoint renderer that router handled rendering.
                args.Path = _notFoundPageRoute;
                RenderNotFound();
            }
            Log.DisplayingNotFound(_logger, args.Path);
        }
    }

    internal void RenderComponentByRoute(RenderTreeBuilder builder, string route)
    {
        var componentType = FindComponentTypeByRoute(route);

        if (componentType is null)
        {
            throw new InvalidOperationException($"No component found for route '{route}'. " +
                $"Ensure the route matches a component with a [Route] attribute.");
        }

        builder.OpenComponent<RouteView>(0);
        builder.AddAttribute(1, nameof(RouteView.RouteData),
            new RouteData(componentType, new Dictionary<string, object>()));
        builder.CloseComponent();
    }

    [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
    internal Type? FindComponentTypeByRoute(string route)
    {
        RefreshRouteTable();
        var normalizedRoute = route.StartsWith('/') ? route : $"/{route}";

        var context = new RouteContext(normalizedRoute);
        Routes.Route(context);

View on GitHub (pinned to 294cab2f9b)

Solutions

  1. Verify the failing route has a matching @page "/exact/path" or [Route("/exact/path")] attribute on a component that implements IComponent.
  2. If the component lives in a separate assembly/project, register it in Router.AppData with additional assemblies or confirm Routes is discovered via the right render mode.
  3. Match the exact casing and leading slash — FindComponentTypeByRoute only prepends '/' when absent, it does not case-fold or strip trailing slashes.
  4. If the route is legitimately absent, route to a NotFound page by leaving args.Path empty rather than passing the unknown path to RenderComponentByRoute.
  5. Run the app with Microsoft.AspNetCore.Components logging at Debug to see which normalized route string is failing to resolve.

Example fix

// before
<Router AppAssembly="@typeof(Program).Assembly">
    <Found>...</Found>
    <NotFound><p>Not found</p></NotFound>
</Router>
// navigation to /old-page that no longer exists throws during OnNotFound

// after — add the page or redirect
@page "/old-page"
@page "/new-page"
<NewPageContent />
Defensive patterns

Strategy: validation

Validate before calling

// Before navigating, confirm the route is registered with the router.
var routeCollection = Router?.Routes; // access via reflection if needed, or maintain a known route set
if (NavigationManager.Uri.StartsWith(NavigationManager.BaseUri))
{
    var rel = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
    // validate against your declared @page list before RenderComponentByRoute is invoked
    if (!KnownRoutes.Contains("/" + rel.Split('?')[0]))
    {
        NavigationManager.NavigateTo("/not-found");
    }
}

Type guard

// Narrow route strings to a known-good set
private static readonly HashSet<string> ValidRoutes = new()
{
    "/", "/counter", "/fetchdata"
};
static bool IsValidRoute(string? route) =>
    route is not null && ValidRoutes.Contains(route.StartsWith('/') ? route : "/" + route);

Try / catch

// Wrap any external subscriber that sets args.Path in OnNotFound
try
{
    _renderHandle.Render(builder => RenderComponentByRoute(builder, requestedPath));
}
catch (InvalidOperationException ex) when (ex.Message.Contains("No component found for route"))
{
    // fall back to the NotFound template instead of crashing the circuit
    RenderNotFound();
}

Prevention

When it happens

Trigger: Calling NavigationManager.NavigateTo or having the Router render a path that no component declares with @page "/that/path". Triggered specifically via the OnNotFound path where args.Path is non-empty and _renderHandle is initialized, invoking RenderComponentByRoute(builder, args.Path) which returns a null componentType. Also reproducible when a route is built dynamically or the RouteContext.Handler is null because the matched type does not implement IComponent.

Common situations: A page was renamed/removed but a link still points at the old URL; the App.razor NotFound template or a custom router subscriber hands a path to the router that has no @page directive; per-page interactivity misconfiguration routes to a component living only in the .Client project that wasn't included in the route discovery assembly; case-sensitivity or trailing-slash differences between the link and the @page literal.

Related errors


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