dotnet/aspnetcore · error · ArgumentException

The value must implement IComponent.

Error message

The value must implement IComponent.

What it means

Thrown by the RouteData constructor. The first argument (pageType) must be a component — it must implement IComponent because RouteData later instantiates that type as a rendered page. Passing a plain class, model, or non-component type fails this guard.

Source

Thrown at src/Components/Components/src/Routing/RouteData.cs:26

/// <summary>
/// Describes information determined during routing that specifies
/// the page to be displayed.
/// </summary>
public sealed class RouteData
{
    /// <summary>
    /// Constructs an instance of <see cref="RouteData"/>.
    /// </summary>
    /// <param name="pageType">The type of the page matching the route, which must implement <see cref="IComponent"/>.</param>
    /// <param name="routeValues">The route parameter values extracted from the matched route.</param>
    public RouteData([DynamicallyAccessedMembers(Component)] Type pageType, IReadOnlyDictionary<string, object?> routeValues)
    {
        ArgumentNullException.ThrowIfNull(pageType);

        if (!typeof(IComponent).IsAssignableFrom(pageType))
        {
            throw new ArgumentException($"The value must implement {nameof(IComponent)}.", nameof(pageType));
        }

        PageType = pageType;
        RouteValues = routeValues ?? throw new ArgumentNullException(nameof(routeValues));
    }

    /// <summary>
    /// Gets the type of the page matching the route.
    /// </summary>
    [DynamicallyAccessedMembers(Component)]
    public Type PageType { get; }

    /// <summary>
    /// Gets route parameter values extracted from the matched route.
    /// </summary>
    public IReadOnlyDictionary<string, object?> RouteValues { get; }

    /// <summary>

View on GitHub (pinned to 294cab2f9b)

Solutions

  1. Filter candidate types with typeof(IComponent).IsAssignableFrom(t) before constructing RouteData.
  2. Ensure the page type is a Blazor component (decorated with @page / [Route] and inherits from ComponentBase or implements IComponent).
  3. If the type genuinely should be a page, make it implement IComponent.

Example fix

// before
var rd = new RouteData(typeof(MyViewModel), values); // MyViewModel is not a component

// after
if (!typeof(IComponent).IsAssignableFrom(typeof(MyPage)))
    throw new InvalidOperationException("page must implement IComponent");
var rd = new RouteData(typeof(MyPage), values);
Defensive patterns

Strategy: type-guard

Validate before calling

static RouteData? SafeRouteData(Type pageType, IReadOnlyDictionary<string,object?> values)
    => typeof(IComponent).IsAssignableFrom(pageType) ? new RouteData(pageType, values) : null;

Type guard

static bool IsComponentType(Type t) => typeof(IComponent).IsAssignableFrom(t);

Prevention

When it happens

Trigger: Calling new RouteData(pageType, routeValues) where pageType is a type that does not implement IComponent (e.g., a ViewModel, a record, or a Razor page handler class). Happens in custom routers or test helpers that build RouteData from reflected [Route] types.

Common situations: A custom routing solution scanning assemblies for [Route]-decorated types and feeding them to RouteData without an IComponent check; a test that constructs RouteData with a stub type; a type annotated with [Route] that is not a Blazor component.

Related errors


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