dotnet/aspnetcore · error · InvalidOperationException

Component parameters may only be added immediately after fra

Error message

Component parameters may only be added immediately after frames of type Component

What it means

Thrown by the private AssertCanAddComponentParameter guard invoked by AddComponentParameter. Component parameters (the [Parameter] values passed to a child component) may only be added immediately after a Component frame — _lastNonAttributeFrameType must be Component. Passing a parameter to an element, text, region, etc. is invalid.

Source

Thrown at src/Components/Components/src/Rendering/RenderTreeBuilder.cs:732

    {
        var indexOfEntryBeingClosed = _openElementIndices.Pop();
        _entries.Buffer[indexOfEntryBeingClosed].RegionSubtreeLengthField = _entries.Count - indexOfEntryBeingClosed;
    }

    private void AssertCanAddAttribute()
    {
        if (_lastNonAttributeFrameType != RenderTreeFrameType.Element
            && _lastNonAttributeFrameType != RenderTreeFrameType.Component)
        {
            throw new InvalidOperationException($"Attributes may only be added immediately after frames of type {RenderTreeFrameType.Element} or {RenderTreeFrameType.Component}");
        }
    }

    private void AssertCanAddComponentParameter()
    {
        if (_lastNonAttributeFrameType != RenderTreeFrameType.Component)
        {
            throw new InvalidOperationException($"Component parameters may only be added immediately after frames of type {RenderTreeFrameType.Component}");
        }
    }

    private int? GetCurrentParentFrameIndex()
        => _openElementIndices.Count == 0 ? (int?)null : _openElementIndices.Peek();

    private RenderTreeFrameType? GetCurrentParentFrameType()
    {
        var parentIndex = GetCurrentParentFrameIndex();
        return parentIndex.HasValue
            ? _entries.Buffer[parentIndex.Value].FrameTypeField
            : (RenderTreeFrameType?)null;
    }

    /// <summary>
    /// Clears the builder.
    /// </summary>
    public void Clear()

View on GitHub (pinned to 294cab2f9b)

Solutions

  1. Ensure AddComponentParameter is called only right after OpenComponent(...) and before CloseComponent().
  2. Use AddAttribute (not AddComponentParameter) when setting HTML element attributes.
  3. Reorder so no content/region/text frame intervenes between OpenComponent and the parameters.

Example fix

// before
builder.OpenElement(0, "div");
builder.AddComponentParameter(1, "Value", 42);
builder.CloseElement();

// after
builder.OpenComponent<MyComp>(0);
builder.AddComponentParameter(1, "Value", 42);
builder.CloseComponent();
Defensive patterns

Strategy: validation

Validate before calling

// AddComponentParameter only after OpenComponent; use AddAttribute for elements.
void Render(RenderTreeBuilder b) {
    b.OpenComponent<MyComp>(0);
    b.AddComponentParameter(1, nameof(MyComp.Value), 42); // ok: parent is Component
    b.CloseComponent();
}

Prevention

When it happens

Trigger: Calling builder.AddComponentParameter(seq, name, value) when the last non-attribute frame is not a Component — e.g., after OpenElement, AddContent, OpenRegion, or at the start. In .razor, a component parameter placed on an HTML element or after content.

Common situations: Mixing up AddAttribute (for elements) and AddComponentParameter (for components); a parameter emitted after a text/region frame; generated code that adds parameters without an OpenComponent.

Related errors


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