Web 方法仅在标记为静态时有效,否则无效

Web method works only if marked as static, otherwise doesn't

代码隐藏:

[ScriptMethod]
[WebMethod]
public string SavePassword(string password, string username, string resellerId, string isPasswordReset)
{    
    return null; // For example
}

javaScript:

$.ajax({
     type: 'POST',
     url: 'login.aspx/SavePassword',
     data: '{"password":"' + passwordInput + '","username":"' + hidusrname + '","resellerId":"' + hidResellerId + '","IsPasswordReset":"' + hidIsPasswordReset + '"}',
     contentType: 'application/json; charset=utf-8',
     dataType: 'json',
     success: function (result) { },
     error: function () { alert('error');
  }

});

public static string SavePassword() 有效,public string SavePassword() 无效。

如果我将 webmethod 更改为静态,它会起作用,如果我不这样做,它就不起作用。

如何在我的页面中使用没有 static 方法的 webmethod?

Web 方法必须是静态的。不可能有实例 web 方法。原因在于它们的设计方式。

Web 方法没有任何状态,也与页面的其余部分没有任何关系。没有理由经历创建页面对象及其所有生命周期的整个 ASP.NET 管道。它们也不需要处理页面对象创建所需的视图状态字段。作为完全脱离页面对象的方法,它们必须声明为静态才能正常运行。

在 return 中,与常规 ASP.NET 页面或更新面板相比,网络方法提供了非常好的性能。