Unknown web method. Parameter name: methodName
Asked Answered
F

8

32

In researching this problem most SO issues were about the static method as a fix.

Since it's not working with the real (and a bit sophisticated) WebMethod I've just created a simple one for the sake of checking if reaching the method itself is possible.

[WebMethod]
[ScriptMethod(UseHttpGet = false)]
public static string HelloWorld()
{
    return "Hello World!";
}

The call.

<script>
    $(document).ready(function () {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "usersWebMethods.aspx/HelloWorld",
            dataType: "json",
            success: function (data) {
                alert(data.d);
            }
        });
   });
</script>

It always comes down to 500 (Internal Server Error)

Unknown web method HelloWorld.
Parameter name: methodName
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.ArgumentException: Unknown web method HelloWorld.
Parameter name: methodName

Why is this failing?

Frameup answered 22/12, 2013 at 23:27 Comment(18)
static web methods are not supported.Amah
maybe I got it all wrong or I haven't explained myself properly. This is just an .aspx page using webmethods in the codebehind. It's not a webservice.Frameup
Ah ok, are you using a ScriptManager? If so do you have the EnableScriptMethods enabled?Amah
No, just using jQuery, I was sure there's no need to use a ScriptManagerFrameup
no you shouldn't have to, I am just trying to gauge your setup. Which page are you running the JS from?Amah
And I thank you for your time! Originally I intend to call the methods from a .html page, but for the sake of testing I'm just running the JS from the .aspx page itself, usersWebMethods.aspx in my case.Frameup
No worries, have you tried passing an empty data param? i.e. data: "{}"?Amah
that I also tried, same result. Right now I'm playing with Fiddler to see if I can dig a bit more, no good outcome yet.Frameup
Code looks good from what I can see, it is as though the web method has not been exposed. Have you tried cleaning/re-building the site?Amah
Same, don't know what else to try :/Frameup
Hmm my initial incling was the URL was wrong, could you try something like url: window.location.href + "/HelloWorld"?Amah
Same :/ it's not being accesed at all.Frameup
I'd opt for going for the old-fashioned approach and use a ScriptManager then.Amah
try adding a error callback and do a console log of the errorSociality
As embarrassing as it is to admit this, my problem was because the method's access modifier was private instead of public....Expand
@Expand it's great once you've found out though. :)Frameup
What was the problem in .aspx page? (In your accepted answer). I'm the 11th person that upvotes the comment under your answer, but no one replied yet.Warship
@Warship for some reason I thought I replied in the past. It's done now, and I'm also updating the answer. ThanksFrameup
F
22

I had a problem in the actual .aspx file, the line

<%@ Page Language="C#" 
         AutoEventWireup="true" 
         CodeBehind="xxx.xxx.cs" Inherits="xxx.xxx" %>

wasn't present in the code. How did it get changed? I Don't know :(.

Frameup answered 24/12, 2013 at 12:55 Comment(5)
wahahhahhah thanks sooooooooooooooooooooooooooooo much. exactly silly spent 3 hours on itAlicaalicante
@INgeeg glad I was of help :)Frameup
What was the problem in .aspx page?Lupine
thanks for admitting it here for those of us who were suffering the same sad situation!Originative
@Lupine the line <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="xxx.xxx.cs" Inherits="xxx.xxx" %> was mistakenly erased from the page. Added it and it worked.Frameup
S
77

I had this issue as well, but slightly differently I had this method in a .asmx file and so ran across the "static" issue, but in a different way.

If you have a method as part of your Page class, it must be static.

If you've put a method in an .asmx file to use across several pages, it must not be static.

Shirleenshirlene answered 17/6, 2014 at 10:46 Comment(3)
I got the same issue while using WebMethod in ASP .NET Page, just made it static and it started working. +1Dwanadwane
converted a page member method to a webmethod and forgot to change scope and member attribute....Logistics
I can't believe that worked! In my asmx page I had a public static method. After removing the static the method was hit successfully! Thank you!Jackelinejackelyn
F
22

I had a problem in the actual .aspx file, the line

<%@ Page Language="C#" 
         AutoEventWireup="true" 
         CodeBehind="xxx.xxx.cs" Inherits="xxx.xxx" %>

wasn't present in the code. How did it get changed? I Don't know :(.

Frameup answered 24/12, 2013 at 12:55 Comment(5)
wahahhahhah thanks sooooooooooooooooooooooooooooo much. exactly silly spent 3 hours on itAlicaalicante
@INgeeg glad I was of help :)Frameup
What was the problem in .aspx page?Lupine
thanks for admitting it here for those of us who were suffering the same sad situation!Originative
@Lupine the line <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="xxx.xxx.cs" Inherits="xxx.xxx" %> was mistakenly erased from the page. Added it and it worked.Frameup
G
3

For me, the primary issues was to change javascript post to pass in no arguments such as

$http.post("Status.aspx/MyData", {})

Then to verify nothing was cached, I then deleted [System.Web.Services.WebMethod] in the code behind file above public static string MyData(). Then I built the project to failure, then re-added the aformentioned deleted attribute and built to success.

Upon running it worked.

Grebe answered 9/6, 2017 at 19:38 Comment(1)
The second part of this worked for me. It was some kind of stupid caching issue. Rename the function by adding "2" to the end (client-side and server side) and it worked! Renamed it back to the original name: STILL working, despite the code now being byte-identical to when it didn't work. And I'd already cleaned out Temporary ASP.NET Files so god knows where it was being cached... (RAM?)Intraatomic
O
3

Missing the [WebMethod] above your server side function will also cause this error.

Overt answered 2/2, 2018 at 21:57 Comment(1)
I've done this a hundred times, and I always forget the attribute. Thanks!Trustful
B
3

To be honest, I've just realised "again" how tired we could be in some cases.

For me it was just a private method instead of a public one.

Bevatron answered 31/8, 2018 at 11:36 Comment(1)
Yes, very tired. Same goes for protected methods.Seclusive
S
1

So the cases may have below based on the answers.

1) check refrence of the code behind page in the top of aspx file <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="xxx.xxx.cs" Inherits="xxx.xxx" %>
2) $http.post("Status.aspx/MyData", {}) forgot to pass the argument and the argument name should be same in client side code(jquery) and aspx.cs method
3) forgot to put [WebMethod] just above the method which comes under System.Web.Services;
4) forgot to use public access specifier
5) forgot to use static keyword in method
6) forgot to compile the code after adding method in running project.

Adding one more case number 6 if some body forgot to compile the code after adding the web method in aspx.cs file.

Suspensory answered 10/5, 2023 at 7:30 Comment(0)
D
0

In my case there was a problem in the URL, it was a Asp.Net Website application:

For ex:

$.ajax({
 type: "POST",
 contentType: "application/json; charset=utf-8",
 url: "usersWebMethods.aspx/HelloWorld",  <----- Here 
 dataType: "json",
 success: function (data) {
    alert(data.d);
 }
});

My usersWebMethods.aspx in inside UI (Custom Created) folder so If I put URL as usersWebMethods.aspx/HelloWorld it does not work but when I added leading / to it then ajax method called properly!

Changed from:

usersWebMethods.aspx/HelloWorld

To

/usersWebMethods.aspx/HelloWorld  --
Deviate answered 21/9, 2019 at 9:36 Comment(0)
S
0

I run into this exact problem in ASP.net(framework/web forms) with JS using webservice and I solved it by removing the static key word from the method declaration

 [WebMethod]
 public List<ViewModel> HelloWorld()
 {
   //Code goes here
 }

instead of

 [WebMethod]
 public static List<ViewModel> HelloWorld()
 {
   //Code goes here
 }
Skatole answered 16/11, 2022 at 20:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.