dotnet/aspnetcore · error · InvalidOperationException
The method cannot be used as an event handler because it…
Error message
The method {methodInfo} cannot be used as an event handler because it declares more than one parameter. What it means
EventArgsTypeCache.GetEventArgsType resolves the event-args type for a C# method used as a Blazor event handler (e.g., @onclick="OnClick"). Blazor event handlers may take zero parameters (treated as EventArgs) or exactly one parameter whose type derives from EventArgs. A method declaring two or more parameters cannot be invoked by the event-dispatch pipeline with a single event argument, so the cache throws at first dispatch.
Solutions
- Reduce the handler to zero or one parameter; capture additional context via closures or fields instead of extra parameters.
- If the method needs more data, wrap it: @onclick="@(() => HandleClick(e, extra))".
- Ensure the chosen overload has at most one EventArgs-derived parameter.
Example fix
// before
void HandleClick(MouseEventArgs e, int extra) { ... }
< button @onclick="HandleClick">...</button>
// after
void HandleClick(MouseEventArgs e) { ... /* use captured 'extra' */ }
<button @onclick="HandleClick">...</button>
// or
<button @onclick="@(() => HandleClick(arg, extra))">...</button> Defensive patterns
Strategy: type-guard
Type guard
static bool IsValidEventHandlerSignature(MethodInfo m)
{
var ps = m.GetParameters();
return ps.Length == 0
|| (ps.Length == 1 && typeof(EventArgs).IsAssignableFrom(ps[0].ParameterType));
}
var method = typeof(MyComponent).GetMethod(nameof(MyComponent.HandleClick));
Assert.True(IsValidEventHandlerSignature(method!)); Prevention
- Keep event-handler methods to zero or one parameter.
- Pass additional context via closures, not extra parameters.
- Add a unit test validating all @on-prefixed handler signatures.
When it happens
Trigger: Wiring @onclick="HandleClick" where HandleClick has signature void HandleClick(MouseEventArgs e, int extra). Using a method with multiple parameters as an event handler in Razor. Passing a delegate whose Invoke has more than one parameter.
Common situations: Adding an extra parameter to an existing event handler (e.g., for context) without refactoring to use a closure or captured state. Reusing a general-purpose method as an event handler. Inadvertently selecting an overload with multiple parameters.
Related errors
- The event handler parameter type
- A public property ' ' on component type ' ' with a public…
- An exception occurred while dispatching a location changed…
- Incorrect frame type
- Multiple properties were found on component type
AI-assisted analysis of dotnet/aspnetcore@3600ca084e (2026-08-11).
Data as JSON: /api/errors/4c5218c42ce5e6ff.
Report an issue: GitHub.
Appendix: source
Thrown at src/Components/Components/src/RenderTree/EventArgsTypeCache.cs:33
{
if (HotReloadManager.IsSupported)
{
HotReloadManager.Default.OnDeltaApplied += Cache.Clear;
}
}
public static Type GetEventArgsType(MethodInfo methodInfo)
{
return Cache.GetOrAdd(methodInfo, methodInfo =>
{
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)