{"record":{"id":"4c5218c42ce5e6ff","repo":"dotnet/aspnetcore","slug":"the-method-methodinfo-cannot-be-used-as-an-event","errorCode":null,"errorMessage":"The method {methodInfo} cannot be used as an event handler because it declares more than one parameter.","messagePattern":"The method (.+?) cannot be used as an event handler because it declares more than one parameter\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Components/src/RenderTree/EventArgsTypeCache.cs","lineNumber":33,"sourceCode":"    {\n        if (HotReloadManager.IsSupported)\n        {\n            HotReloadManager.Default.OnDeltaApplied += Cache.Clear;\n        }\n    }\n\n    public static Type GetEventArgsType(MethodInfo methodInfo)\n    {\n        return Cache.GetOrAdd(methodInfo, methodInfo =>\n        {\n            var parameterInfos = methodInfo.GetParameters();\n            if (parameterInfos.Length == 0)\n            {\n                return typeof(EventArgs);\n            }\n            else if (parameterInfos.Length > 1)\n            {\n                throw new InvalidOperationException($\"The method {methodInfo} cannot be used as an event handler because it declares more than one parameter.\");\n            }\n            else\n            {\n                var declaredType = parameterInfos[0].ParameterType;\n                if (typeof(EventArgs).IsAssignableFrom(declaredType))\n                {\n                    return declaredType;\n                }\n                else\n                {\n                    throw new InvalidOperationException($\"The event handler parameter type {declaredType.FullName} for event must inherit from {typeof(EventArgs).FullName}.\");\n                }\n            }\n        });\n    }\n}\n","sourceCodeStart":15,"sourceCodeEnd":50,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Components/Components/src/RenderTree/EventArgsTypeCache.cs#L15-L50","documentation":"Blazor event handlers (@onclick, @oninput, etc.) reference C# methods whose signatures the framework inspects via EventArgsTypeCache.GetEventArgsType. When a method declares more than one parameter, the framework cannot determine which parameter represents the event argument, so it throws InvalidOperationException. Event handlers must accept zero parameters (action-style) or exactly one parameter (the EventArgs-derived argument).","triggerScenarios":"Using a method reference (not a lambda) as a Blazor event handler where the method has two or more parameters. For example, @onclick=\"MyHandler\" where MyHandler is defined as void MyHandler(MouseEventArgs e, string extra). The framework resolves the event argument type at bind time via reflection and rejects multi-parameter methods.","commonSituations":"Trying to pass additional context to a handler via extra method parameters instead of a lambda closure; refactoring a method signature and adding parameters without updating event bindings; misunderstanding that Blazor event handlers can only receive the event args.","solutions":["If the handler needs additional context, wrap it in a lambda: @onclick=\"@(() => MyHandler(arg1, arg2))\".","If the handler needs the event args plus extra data, capture the extra data in a closure: @onclick=\"@(e => MyHandler(e, extraData))\".","Reduce the method to zero or one parameter to match the Blazor event handler contract.","If zero parameters are needed for the action-style handler, remove the extra parameters and use closures to pass data."],"exampleFix":"<!-- before -->\n@onclick=\"OnItemClick\"\n@code {\n    void OnItemClick(MouseEventArgs e, int itemId) { } // two params\n}\n\n<!-- after -->\n@onclick=\"@(e => OnItemClick(e, itemId))\"\n@code {\n    void OnItemClick(MouseEventArgs e, int itemId) { }\n}","handlingStrategy":"validation","validationCode":"// Validate event handler method signature before binding\nstatic bool IsValidEventHandler(MethodInfo method)\n{\n    var parms = method.GetParameters();\n    return parms.Length <= 1;\n}\n// Use: if (IsValidEventHandler(typeof(MyComp).GetMethod(\"OnClick\"))) { ... }","typeGuard":"// In Razor, prefer lambdas for handlers needing extra context:\n// @onclick=\"@(e => HandleClick(e, itemId))\"","tryCatchPattern":null,"preventionTips":["Use lambda expressions for event handlers that need extra context, rather than multi-parameter methods.","Keep event handler methods to zero or one (EventArgs-derived) parameters.","Run a build-time Roslyn analyzer to flag methods used as handlers with >1 parameter.","Review method signatures before referencing them in @on* attributes."],"tags":["blazor","event-handler","reflection","razor","parameters"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}