Reputation: 2074
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?
Upvotes: 32
Views: 45536
Reputation: 191
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.
Upvotes: 1
Reputation: 83
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
}
Upvotes: 0
Reputation: 10697
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 --
Upvotes: 0
Reputation: 416
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.
Upvotes: 3
Reputation: 3657
Missing the [WebMethod]
above your server side function will also cause this error.
Upvotes: 3
Reputation: 2074
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 :(.
Upvotes: 22
Reputation: 6943
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
.
Upvotes: 77
Reputation: 31606
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.
Upvotes: 3