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

问题描述 投票:0回答:7

在研究这个问题时,大多数 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 (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

为什么会失败?

c# asp.net ajax pagemethods
7个回答
75
投票

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

如果你有一个方法作为你的页面类的一部分,它必须

static
.

如果您在 .asmx 文件中放置了一个方法以跨多个页面使用,它一定不能

static
.


22
投票

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

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

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


3
投票

对我来说,主要问题是更改 javascript

post
以不传递任何参数,例如

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

然后验证没有缓存,然后我删除了

[System.Web.Services.WebMethod]
上面的代码隐藏文件中的
public static string MyData()
。然后我构建项目失败,然后重新添加上述删除的属性并构建成功。

运行后它起作用了。


3
投票

缺少服务器端功能上方的

[WebMethod]
也会导致此错误。


3
投票

老实说,我刚刚“再次”意识到在某些情况下我们会多么疲倦。

对我来说,这只是一个

private
方法,而不是一个
public
方法。


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.aspx
UI
(自定义创建)文件夹中所以如果我把URL作为
usersWebMethods.aspx/HelloWorld
它不起作用但是当我添加前导
/
它然后ajax方法正确调用!

更改自:

usersWebMethods.aspx/HelloWorld

/usersWebMethods.aspx/HelloWorld  --

0
投票

我在 ASP.net(framework/web forms) 中使用 webservice 的 JS 遇到了这个确切的问题 我通过从方法声明中删除

static
关键字解决了它

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

代替

 [WebMethod]
 public static List<ViewModel> HelloWorld()
 {
   //Code goes here
 }
© www.soinside.com 2019 - 2024. All rights reserved.