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?

Daniel Sh.
  • 2,078
  • 5
  • 42
  • 67
  • 3
    `static` web methods are [not supported](http://stackoverflow.com/questions/1263379/why-are-static-methods-not-usable-as-web-service-operations-in-asmx-web-services). – James Dec 22 '13 at 23:34
  • 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. – Daniel Sh. Dec 22 '13 at 23:38
  • Ah ok, are you using a ScriptManager? If so do you have the [EnableScriptMethods](http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.enablepagemethods(v=vs.110).aspx) enabled? – James Dec 22 '13 at 23:49
  • No, just using jQuery, I was sure there's no need to use a [ScriptManager](http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/) – Daniel Sh. Dec 22 '13 at 23:56
  • no you shouldn't have to, I am just trying to gauge your setup. Which page are you running the JS from? – James Dec 23 '13 at 00:16
  • 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. – Daniel Sh. Dec 23 '13 at 00:18
  • No worries, have you tried passing an empty `data` param? i.e. `data: "{}"`? – James Dec 23 '13 at 00:21
  • 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. – Daniel Sh. Dec 23 '13 at 00:22
  • 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? – James Dec 23 '13 at 00:23
  • Same, don't know what else to try :/ – Daniel Sh. Dec 23 '13 at 00:30
  • Hmm my initial incling was the URL was wrong, could you try something like `url: window.location.href + "/HelloWorld"`? – James Dec 23 '13 at 00:35
  • Same :/ it's not being accesed at all. – Daniel Sh. Dec 23 '13 at 01:05
  • I'd opt for going for the old-fashioned approach and use a ScriptManager then. – James Dec 23 '13 at 01:07
  • try adding a error callback and do a console log of the error – iJade Dec 23 '13 at 10:07
  • 2
    As embarrassing as it is to admit this, my problem was because the method's access modifier was private instead of public.... – JWiley Aug 17 '15 at 12:54
  • @JWiley it's great once you've found out though. :) – Daniel Sh. Sep 08 '15 at 17:57
  • 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. – Artemix Jan 23 '17 at 17:07
  • @Artemix for some reason I thought I replied in the past. It's done now, and I'm also updating the answer. Thanks – Daniel Sh. Jan 23 '17 at 17:33

8 Answers8

76

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.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Rob Church
  • 6,783
  • 3
  • 41
  • 46
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 :(.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Daniel Sh.
  • 2,078
  • 5
  • 42
  • 67
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.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • 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?) – NickG Feb 22 '19 at 10:51
3

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

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
hogarth45
  • 3,387
  • 1
  • 22
  • 27
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.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
smlnl
  • 396
  • 5
  • 15
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  --
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
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
 }
Abdi fatah
  • 53
  • 6
0

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.

Aman Attari
  • 181
  • 3
  • 12