dotnet/aspnetcore · error · ArgumentNullException

layoutType

Error message

layoutType

What it means

LayoutAttribute's constructor requires a non-null layout type. A null layout type is meaningless and is rejected with ArgumentNullException.

Source

Thrown at src/Components/Components/src/LayoutAttribute.cs:21

using System.Diagnostics.CodeAnalysis;
using static Microsoft.AspNetCore.Internal.LinkerFlags;

namespace Microsoft.AspNetCore.Components;

/// <summary>
/// Indicates that the associated component type uses a specified layout.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public sealed class LayoutAttribute : Attribute
{
    /// <summary>
    /// Constructs an instance of <see cref="LayoutAttribute"/>.
    /// </summary>
    /// <param name="layoutType">The type of the layout.</param>
    public LayoutAttribute([DynamicallyAccessedMembers(Component)] Type layoutType)
    {
        LayoutType = layoutType ?? throw new ArgumentNullException(nameof(layoutType));

        if (!typeof(IComponent).IsAssignableFrom(layoutType))
        {
            throw new ArgumentException($"Invalid layout type: {layoutType.FullName} " +
                $"does not implement {typeof(IComponent).FullName}.");
        }

        // Note that we can't validate its acceptance of a 'Body' parameter at this stage,
        // because the contract doesn't force them to be known statically. However it will
        // be a runtime error if the referenced component type rejects the 'Body' parameter
        // when it gets used.
    }

    /// <summary>
    /// The type of the layout. The type must implement <see cref="IComponent"/>
    /// and must accept a parameter with the name 'Body'.
    /// </summary>
    [DynamicallyAccessedMembers(Component)]

View on GitHub (pinned to 3600ca084e)

Solutions

  1. Pass a concrete layout type (a Razor component) to LayoutAttribute.
  2. Omit the Layout attribute entirely if no layout is needed.
  3. Resolve the layout type via a non-null default before applying the attribute.

Example fix

// before
[Layout(null)]
// after
[Layout(typeof(MainLayout))]
Defensive patterns

Strategy: validation

Validate before calling

ArgumentNullException.ThrowIfNull(layoutType);
var attr = new LayoutAttribute(layoutType);

Prevention

When it happens

Trigger: [Layout(null)] on a component, or programmatically new LayoutAttribute(null).

Common situations: Dynamic layout type that resolves to null, refactoring that leaves a null reference, or misconfigured layout selection logic.

Related errors


AI-assisted analysis of dotnet/aspnetcore@3600ca084e (2026-08-11). Data as JSON: /api/errors/4c6f02a88d5c9481. Report an issue: GitHub.