{"record":{"id":"0d52cd1eefbf30a9","repo":"dotnet/aspnetcore","slug":"no-component-found-for-route-route-ensure-the","errorCode":null,"errorMessage":"No component found for route '{route}'. Ensure the route matches a component with a [Route] attribute.","messagePattern":"No component found for route '(.+?)'\\. Ensure the route matches a component with a \\[Route\\] attribute\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Components/src/Routing/Router.cs","lineNumber":416,"sourceCode":"                _renderHandle.Render(builder => RenderComponentByRoute(builder, args.Path));\n            }\n            else\n            {\n                // Having the path set signals to the endpoint renderer that router handled rendering.\n                args.Path = _notFoundPageRoute;\n                RenderNotFound();\n            }\n            Log.DisplayingNotFound(_logger, args.Path);\n        }\n    }\n\n    internal void RenderComponentByRoute(RenderTreeBuilder builder, string route)\n    {\n        var componentType = FindComponentTypeByRoute(route);\n\n        if (componentType is null)\n        {\n            throw new InvalidOperationException($\"No component found for route '{route}'. \" +\n                $\"Ensure the route matches a component with a [Route] attribute.\");\n        }\n\n        builder.OpenComponent<RouteView>(0);\n        builder.AddAttribute(1, nameof(RouteView.RouteData),\n            new RouteData(componentType, new Dictionary<string, object>()));\n        builder.CloseComponent();\n    }\n\n    [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]\n    internal Type? FindComponentTypeByRoute(string route)\n    {\n        RefreshRouteTable();\n        var normalizedRoute = route.StartsWith('/') ? route : $\"/{route}\";\n\n        var context = new RouteContext(normalizedRoute);\n        Routes.Route(context);\n","sourceCodeStart":398,"sourceCodeEnd":434,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/Components/Components/src/Routing/Router.cs#L398-L434","documentation":"RenderComponentByRoute resolves the requested route to a component type via FindComponentTypeByRoute; if no component in the configured assemblies declares a matching [Route], the type lookup returns null and the method throws InvalidOperationException with a hint to verify the [Route] attribute. This is the explicit failure mode for the programmatic render-by-route API (used by tests and Server/renderer entry points).","triggerScenarios":"Calling RenderComponentByRoute (or an internal caller such as the test harness / pre-rendering pipeline) with a route string that matches no [Route] attribute in the scanned assemblies. Also triggered by navigation to a path before the page component has been registered (race with route table population).","commonSituations":"Programmatically rendering a component by URL in a test and mistyping the route; the target page is in an assembly not passed to the Router; the [Route] uses a parameter placeholder that does not match the supplied literal route; trimming removed the page or its [Route] attribute at publish time.","solutions":["Confirm the route string exactly matches a [Route(\"...\")] on a component, including leading slash and parameter placeholders.","Add the assembly containing the page to AppAssembly / AdditionalAssemblies on the Router.","If publishing with trimming, ensure the page type and its [Route] attribute are root-preserved (e.g. via DynamicallyAccessedMembers or a linker XML).","Verify the page component compiles and its generated route attribute is present (check with reflection in a test)."],"exampleFix":"// before\nrenderer.RenderComponentByRoute(\"/usrs/42\");  // typo\n// after\nrenderer.RenderComponentByRoute(\"/users/42\"); // matches [Route(\"/users/{id}\")]","handlingStrategy":"validation","validationCode":"// Validate the route is known before invoking RenderComponentByRoute.\nif (!knownRoutes.Contains(route, StringComparer.OrdinalIgnoreCase))\n    throw new InvalidOperationException($\"No registered route matches '{route}'.\");","typeGuard":null,"tryCatchPattern":"// Only meaningful in test harnesses; otherwise the route should already be validated.\ntry { renderer.RenderComponentByRoute(route); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"No component found for route\"))\n{\n    // log and fall back to a known route\n}","preventionTips":["Keep a list of valid routes (derived from [Route] attributes) and validate before programmatic render.","Include every page-bearing assembly in the Router configuration.","When publishing trimmed, root-preserve page types and their [Route] attributes.","Add a test enumerating routes and rendering each."],"tags":["blazor","routing","router","render-by-route","invalidoperationexception"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}