dotnet/aspnetcore · error · InvalidOperationException

The event handler parameter type

Error message

The event handler parameter type {declaredType.FullName} for event must inherit from {typeof(EventArgs).FullName}.

What it means

The second guard in EventArgsTypeCache.GetEventArgsType: when a handler has exactly one parameter, that parameter's type must derive from EventArgs (or be EventArgs itself) so the event-dispatch pipeline can pass the matching event payload. If the single parameter is, for example, int, string, or a custom non-EventArgs type, the cache cannot infer a valid event-args type and throws.

Solutions

  1. Change the handler's parameter to an EventArgs-derived type matching the event (e.g., MouseEventArgs for @onclick, KeyboardEventArgs for @onkeydown, or plain EventArgs).
  2. If the handler needs additional data, capture it via closure and keep only the EventArgs parameter.
  3. For parameterless handling, remove the parameter entirely so the cache treats it as EventArgs.

Example fix

// before
void HandleClick(int id) { ... }
<button @onclick="HandleClick">...</button>

// after
void HandleClick(MouseEventArgs e) { ... /* use captured id */ }
<button @onclick="HandleClick">...</button>
Defensive patterns

Strategy: type-guard

Type guard

static bool IsEventHandlerParamValid(MethodInfo m)
{
    var ps = m.GetParameters();
    return ps.Length == 0
        || (ps.Length == 1 && typeof(EventArgs).IsAssignableFrom(ps[0].ParameterType));
}

Assert.True(IsEventHandlerParamValid(typeof(MyComponent).GetMethod(nameof(MyComponent.HandleClick))!));

Prevention

When it happens

Trigger: Wiring @onclick="HandleClick" where HandleClick is declared as void HandleClick(int id) or void HandleClick(MyDto data) with MyDto not deriving from EventArgs. The first parameter type fails typeof(EventArgs).IsAssignableFrom(declaredType).

Common situations: Using a domain object as the event-handler parameter instead of MouseEventArgs/KeyboardEventArgs/EventArgs. Migrating a callback signature that previously took a plain value. Selecting an overload whose parameter is a non-EventArgs type.

Related errors


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

Appendix: source

Thrown at src/Components/Components/src/RenderTree/EventArgsTypeCache.cs:44

            var parameterInfos = methodInfo.GetParameters();
            if (parameterInfos.Length == 0)
            {
                return typeof(EventArgs);
            }
            else if (parameterInfos.Length > 1)
            {
                throw new InvalidOperationException($"The method {methodInfo} cannot be used as an event handler because it declares more than one parameter.");
            }
            else
            {
                var declaredType = parameterInfos[0].ParameterType;
                if (typeof(EventArgs).IsAssignableFrom(declaredType))
                {
                    return declaredType;
                }
                else
                {
                    throw new InvalidOperationException($"The event handler parameter type {declaredType.FullName} for event must inherit from {typeof(EventArgs).FullName}.");
                }
            }
        });
    }
}

View on GitHub (pinned to 3600ca084e)