在研究这个问题的过程中,大多数问题都是关于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

为什么会失败?

推荐答案

我在实际工作中遇到了问题.aspx文件,行

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

代码中没有.它是怎么变的?我不知道.

Asp.net相关问答推荐

在这个 For 循环计数没有增加

在 AppConfig.json 中存储对象的最佳方式

C# ASP.NET Core Serilog 添加类名和方法到日志(log)

我可以在 .net 核心中使用 Entity Framework 6(非核心)吗?

为 Web 请求实现速率限制算法的最佳方法是什么?

ASP.NET Core 1.0 ConfigurationBuilder().AddJsonFile("appsettings.json");找不到文件

System.Web.Helpers.Crypto - 盐在哪里?

在 C# 中以编程方式添加元标记

使用 ASP.NET WebForms 的 jQuery DataTables 服务器端处理

更改后未应用 CSS

如何在 ASP.NET 中仅在调试模式下执行代码

ASP.NET 中的全局资源与本地资源

发布网站项目时临时路径太长

IIS Express - 增加内存限制

如何防止aspxerrorpath作为查询字符串传递给 ASP.NET 自定义错误页面

在 Application_BeginRequest 中设置会话变量

elmah:没有 HttpContext 的异常?

简单的 LINQ 和列表错误:WhereListIterator`1[Task]' to type 'System.Collections.Generic.List`1[Task]'

在后面的代码中删除 css 类

我可以在加载事件时从 C# 的 div 标记中动态添加 HTML 吗?