dotnet/aspnetcore · error · InvalidOperationException
The {nameof(RouteView)} component requires a non-null value
Error message
The {nameof(RouteView)} component requires a non-null value for the parameter {nameof(RouteData)}. What it means
RouteView is the component that renders the matched page inside a Router; it reads its RouteData parameter and dispatches to the page component described there. A null RouteData is a configuration error: RouteView has nothing to render and cannot guess one, so SetParametersAsync throws InvalidOperationException before requesting a render. The contract is that the host (Router) always supplies RouteData.
Source
Thrown at src/Components/Components/src/RouteView.cs:60
/// and accept a parameter named <see cref="LayoutComponentBase.Body"/>.
/// </summary>
[Parameter]
public Type DefaultLayout { get; set; }
/// <inheritdoc />
public void Attach(RenderHandle renderHandle)
{
_renderHandle = renderHandle;
}
/// <inheritdoc />
public Task SetParametersAsync(ParameterView parameters)
{
parameters.SetParameterProperties(this);
if (RouteData == null)
{
throw new InvalidOperationException($"The {nameof(RouteView)} component requires a non-null value for the parameter {nameof(RouteData)}.");
}
_renderHandle.Render(Render);
return Task.CompletedTask;
}
/// <summary>
/// Renders the component.
/// </summary>
/// <param name="builder">The <see cref="RenderTreeBuilder"/>.</param>
[UnconditionalSuppressMessage("Trimming", "IL2110", Justification = "Layout components are preserved because the LayoutAttribute constructor parameter is correctly annotated.")]
[UnconditionalSuppressMessage("Trimming", "IL2111", Justification = "Layout components are preserved because the LayoutAttribute constructor parameter is correctly annotated.")]
[UnconditionalSuppressMessage("Trimming", "IL2118", Justification = "Layout components are preserved because the LayoutAttribute constructor parameter is correctly annotated.")]
protected virtual void Render(RenderTreeBuilder builder)
{
var pageLayoutType = _layoutAttributeCache
.GetOrAdd(RouteData.PageType, static type => type.GetCustomAttribute<LayoutAttribute>()?.LayoutType)
?? DefaultLayout;View on GitHub (pinned to 3600ca084e)
Solutions
- Confirm RouteView is always rendered with a non-null RouteData — typically <RouteView RouteData="@routeData" /> inside the Router's Found template.
- If using RouteView standalone, pass a RouteData constructed from a known page type.
- Audit the Found render fragment on the parent Router to ensure routeData (the lambda parameter) is forwarded, not shadowed.
- Add a null check at the call site and fall back to a known route rather than letting RouteView receive null.
Example fix
// before (App.razor)
<Found Context="routeData">
<RouteView /> <!-- RouteData omitted, throws -->
</Found>
// after
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found> Defensive patterns
Strategy: validation
Validate before calling
// Before rendering RouteView (e.g. in a custom host), confirm RouteData is non-null.
if (routeData is null)
throw new InvalidOperationException("RouteData must be supplied to RouteView.");
builder.OpenComponent<RouteView>(0);
builder.AddAttribute(1, nameof(RouteView.RouteData), routeData);
builder.CloseComponent(); Type guard
static bool IsValidRouteData(RouteData? d) => d?.PageType is not null;
Prevention
- Always forward the Found lambda's routeData parameter to <RouteView RouteData="@routeData" />.
- When using RouteView standalone, construct a RouteData from a known page type before passing it.
- Add a default value or guard in markup: @if (routeData is not null) { <RouteView RouteData=@routeData /> }.
- Keep RouteView inside the Router's Found template.
When it happens
Trigger: Declaring <RouteView /> in markup without a RouteData parameter, or binding RouteData to an expression that evaluates to null. Also triggered by manually constructing a RouteView and calling SetParametersAsync with a ParameterView that omits RouteData.
Common situations: Removing the @bind or [Parameter] wiring for RouteData in a custom App.razor; a typo in the parameter name causing null binding; using RouteView outside a Router without supplying RouteData manually; reflection-based component activation that skips the parameter.
Related errors
- Unable to find the provided template '{template}'
- The following routes are ambiguous: '{existingText}' in '{ex
- The {nameof(Router)} component requires a value for the para
- The {nameof(Router)} component requires a value for the para
- Setting {nameof(NotFound)} and {nameof(NotFoundPage)} proper
AI-assisted analysis of dotnet/aspnetcore@3600ca084e (2026-08-11).
Data as JSON: /api/errors/70a09a30a08812c9.
Report an issue: GitHub.