简体   繁体   English

Web 服务方法名称无效

[英]Web Service method name is not valid

I get the following error "Web Service method name is not valid" when i try to call webmethod from javascript当我尝试从 javascript 调用 webmethod 时,出现以下错误“Web 服务方法名称无效”

System.InvalidOperationException: SaveBOAT Web Service method name is not valid. System.InvalidOperationException:SaveBOAT Web 服务方法名称无效。 at System.Web.Services.Protocols.HttpServerProtocol.Initialize() at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response) at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)在 System.Web.Services.Protocols.HttpServerProtocol.Initialize() 在 System.Web.Services.Protocols.ServerProtocol.SetContext(类型类型,HttpContext 上下文,HttpRequest 请求,HttpResponse 响应)在 System.Web.Services.Protocols.ServerProtocolFactory。创建(类型类型、HttpContext 上下文、HttpRequest 请求、HttpResponse 响应、Boolean&abortProcessing)

HTML Code : HTML代码:

<asp:LinkButton runat="server" ID="lnkAddBoat" OnClientClick="javascript:AddMyBoat(); return false;"></asp:LinkButton>

JS Code : JS代码:

function AddMyBoat() {
            var b = document.getElementById('HdnControlId').value;

            jQuery.ajax({
                type: "GET",
                url: "/AllService.asmx/SaveBOAT",
                data: { Pid: b },
                contentType: "application/text",
                dataType: "text",
                success: function(dd) {
                    alert('Success' + dd);
                },
                error: function(dd) {
                    alert('There is error' + dd.responseText);
                }
            });
}

C# Code (Web method in AllService.asmx file) C# 代码(AllService.asmx 文件中的 Web 方法)

[WebMethod]
public static string SaveBOAT(int Pid)
{
    // My Code is here
    //I can put anythng here
    SessionManager.MemberID = Pid;
    return "";
}

I tried all solutions found on Stack Overflow and ASP.NET site.but none of them worked for me.我尝试了在 Stack Overflow 和 ASP.NET 站点上找到的所有解决方案。但没有一个对我有用。

It was a silly mistake.这是一个愚蠢的错误。

remove Static keyword from method declaration.从方法声明中删除Static关键字。

[WebMethod]
public string SaveBOAT(string Pid)
{        
     SessionManager.MemberID = Pid;
     return "";
}

在我的情况下,我复制了另一个 asmx 文件,但没有将类属性更改为 asmx 文件本身中新类的名称(右键单击 asmx 文件 -> 查看标记)

在我的情况下,错误是 Web 服务方法被声明为“私有”而不是“公共”

Try using this, I think datatype should be JSON尝试使用这个,我认为数据类型应该是 JSON

       jQuery.ajax({
            type: "POST",  // or GET
            url: "/AllService.asmx/SaveBOAT",
            data: { Pid: b },
            contentType: "application/json; charset=utf-8",
            dataType: "json"
            success: function(dd) {
                alert('Success' + dd);
            },
            error: function(dd) {
                alert('There is error' + dd.responseText);
            }
        });

And in C# Code change Pid to string并在 C# 代码中将 Pid 更改为字符串

    [WebMethod]
     public static string SaveBOAT(string Pid)
     {        
      SessionManager.MemberID = Pid;
      return "";
     }

I too faced the similar issue.我也遇到了类似的问题。 The solution includes checking everything related to ensuring all name, parameters are passed correctly as many have responded.该解决方案包括检查与确保所有名称、参数都正确传递的所有相关内容,因为许多人已做出响应。 Make sure that the web method name that we are calling in UI page is spelled correctly, the data, data types are correct and etc. In my case, I misspelled the web method name in my ajax call.确保我们在 UI 页面中调用的 web 方法名称拼写正确,数据、数据类型正确等等。就我而言,我在 ajax 调用中拼错了 web 方法名称。 It works fine once I found and corrected the name correctly.一旦我找到并正确更正了名称,它就可以正常工作。
For Ex: In .asmx class file, this is the method name "IsLeaseMentorExistWithTheSameName" but when I called from UI this is how I called:例如:在 .asmx 类文件中,这是方法名称“IsLeaseMentorExistWithTheSameName”但是当我从 UI 调用时,我是这样调用的:

var varURL = <%=Page.ResolveUrl("~/Main/BuildCriteria.asmx") %> + '/IsLeaseMentorExistWithSameName';  

Notice that the word "The" is missing.请注意,缺少“The”一词。 That was a mistake and I corrected and so it worked fine.那是一个错误,我纠正了,所以效果很好。

Did U add ServiceReference Class.您是否添加了ServiceReference类。 Check this once.检查一次。 Based on your comment I can tell what to do根据您的评论,我可以告诉您该怎么做

I had this issue because my soap method had a List<string> parameter.我遇到这个问题是因为我的 soap 方法有一个List<string>参数。 Couldn't figure out a way to make it work with the array parameter;无法想出一种方法使其与数组参数一起工作; so just converted the parameter to a & -delimited string (eg val1&val2&val3 ) and converted the parameter to an array in the service method.所以只需将参数转换为&分隔的字符串(例如val1&val2&val3 )并将参数转换为服务方法中的数组。

As Sundar Rajan states, check the parameters are also correct.正如 Sundar Rajan 所说,检查参数是否正确。 My instance of this error was because I had failed to pass any parameters (as a body in a POST request) and the asmx web method was expecting a named parameter, because of this the binding logic failed to match up the request to the method name, even though the name itself is actually correct.我的此错误实例是因为我未能传递任何参数(作为 POST 请求中的主体)并且 asmx Web 方法需要一个命名参数,因此绑定逻辑无法将请求与方法名称匹配,即使名称本身实际上是正确的。

[WebMethod]
        public object MyWebMethod(object parameter)

If there is no parameter in the body of the request then you will get this error.如果请求正文中没有parameter ,那么您将收到此错误。

In my case, one of the WebService receiving parameters was called aId .就我而言,WebService 接收参数之一称为aId When I called it from javascript, I was sending the correct Id value, but the name of the sent variable was incorrectly called bId .当我从 javascript 调用它时,我发送了正确的 Id 值,但发送的变量的名称被错误地称为bId So I just had to rename the WebService call, keep the correct value like before, and just change the variable name.所以我只需要重命名 WebService 调用,像以前一样保持正确的值,然后更改变量名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM