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

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

由于它不使用真正的(有点复杂的)WebMethod,我刚刚创建了一个简单的WebMethod,以便检查是否可以实现方法本身。

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

电话。

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

它总是降至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 

为什么会失败?

我也有这个问题,但稍微不同的是我在.asmx文件中使用了这个方法,因此遇到了“静态”问题,但是以不同的方式。

如果您有一个方法作为您的Page类的一部分,它必须static

如果您已将方法放在.asmx文件中以跨多个页面使用,则它不能static

我在实际的.aspx文件中遇到了问题

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

代码中没有出现。 它是如何改变的? 我不知道 :(。

对我来说,主要的问题是改变javascript post传递没有参数,如

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

然后为了validation没有缓存,我在public static string MyData()上面的代码隐藏文件中删除了[System.Web.Services.WebMethod] 。 然后我将项目构建为失败,然后重新添加了前面提到的已删除属性并构建成功。

在运行它工作。

缺少服务器端function上方的[WebMethod]也会导致此错误。

说实话,我刚刚再次意识到在某些情况下我们会有多累。

对我来说,它只是一种“私人”方法,而不是公共方法。