未知的网络方法。参数名称:方法名

32

在研究这个问题时,大多数SO问题都是关于将static方法作为修复方法。

由于它不能与真正的(有点复杂的)WebMethod一起使用,我只是创建了一个简单的WebMethod来检查是否可以访问该方法本身。

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

电话。

<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>

问题总是归结为500(服务器内部错误)

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

为什么会失败?


3
static的网络方法不受支持 - James
不,你不必这样做,我只是想了解你的设置。你从哪个页面运行JS? - James
我感谢您的时间!最初我打算从一个.html页面调用方法,但为了测试起见,我只是在.aspx页面本身中运行JS,我的情况下是usersWebMethods.aspx - Daniel Sh.
别担心,你试过传递一个空的 data 参数了吗?比如 data: "{}" - James
2
尽管这很令人尴尬,但我的问题是因为该方法的访问修饰符是私有的而不是公共的... - JWiley
显示剩余13条评论
8个回答

76

我也遇到了这个问题,但是略有不同。我的方法在一个.asmx文件中,因此遇到了“静态(static)”的问题,但是方式不同。

如果你把一个方法作为页面类(Page class)的一部分,在这种情况下它必须static的。

如果你把一个方法放在一个.asmx文件中,以便在多个页面中使用,那么它不能static的。


1
我在使用ASP .NET页面中的WebMethod时遇到了相同的问题,只需将其设置为静态即可使其正常工作。+1 - Mohammed Dawood Ansari
将页面成员方法转换为WebMethod时,忘记更改范围和成员属性... - GoldBishop
我简直不敢相信它起作用了!在我的asmx页面中,我有一个公共静态方法。在去掉static关键字后,该方法成功被调用!谢谢! - Lukas

22

我在实际的.aspx文件中遇到了问题,出错代码行为:

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

代码中原本没有这个。它是如何被修改的?我不知道 :(。


很高兴能帮到你,@Rahul :) - Daniel Sh.
1
哇哈哈哈哈,非常感谢。真是太傻了,竟然花了三个小时在这上面。 - InGeek
@INgeeg 很高兴我能帮到你 :) - Daniel Sh.
11
.aspx页面出了什么问题? - LacOniC
谢谢你在这里承认,让我们这些遭受同样悲惨情况的人感到欣慰! - msulis
@LacOniC 这行代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="xxx.xxx.cs" Inherits="xxx.xxx" %> 被错误地从页面中删除了。添加它后,代码正常工作。 - Daniel Sh.

3

说实话,我又意识到在某些情况下我们可能会感到非常疲惫。

对我来说,这只是一个私有方法而不是公共方法。


是的,非常累。对于“protected”方法也是如此。 - Protector one

3

对我来说,首要问题是更改javascript中的post以传递无参数,例如

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

然后,在删除代码后台文件中[System.Web.Services.WebMethod]之前的public static string MyData()之前,验证是否存在缓存。然后我将项目构建到失败,然后重新添加了上述已删除的属性,并构建为成功。

运行后它可以正常工作。


1
这个问题的第二部分对我起作用了。它是某种愚蠢的缓存问题。通过在函数名称末尾添加“2”(客户端和服务器端),它就可以工作了!将其重命名回原始名称:仍然工作,尽管代码现在与不工作时完全相同。而且我已经清除了临时ASP.NET文件,所以上帝知道它被缓存在哪里...(内存?) - NickG

3

如果在服务器端的函数上方缺少[WebMethod],也会导致此错误。


1
我已经做了这个一百次,但我总是忘记这个属性。谢谢! - John Baker

0

我在使用ASP.net(framework/web forms)和JS通过webservice时遇到了这个确切的问题,我通过从方法声明中移除static关键字来解决它。

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

替代

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

0
在我的情况下,URL存在问题,它是一个Asp.Net网站应用程序:
例如:
$.ajax({
 type: "POST",
 contentType: "application/json; charset=utf-8",
 url: "usersWebMethods.aspx/HelloWorld",  <----- Here 
 dataType: "json",
 success: function (data) {
    alert(data.d);
 }
});

我的usersWebMethods.aspxUI(自定义创建)文件夹中,因此如果我将URL设置为usersWebMethods.aspx/HelloWorld,它不起作用,但是当我在前面添加了/时,ajax方法被正确调用!

已更改为:

usersWebMethods.aspx/HelloWorld

/usersWebMethods.aspx/HelloWorld  --

0

因此,根据答案,情况可能如下所述。

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.

如果有人在aspx.cs文件中添加了Web方法,但忘记编译代码,则需要添加另一个案例编号6。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接