请求格式无法识别的URL意外结束于

这不是一个问题 – 张贴在这里供参考:

在使用WebService时,出现以下错误:

对于意外以/ myMethodName结尾的URL,请求格式无法识别

在这个网站上find了解决scheme

所有你需要的是添加以下到你的web.config

<configuration> <system.web> <webServices> <protocols> <add name="HttpGet"/> <add name="HttpPost"/> </protocols> </webServices> </system.web> </configuration> 

来自微软的更多信息

尽pipe我find的所有信息中有90%(试图find解决这个错误的方法)告诉我把HttpGetHttpPost添加到configuration中,但这对我来说并不起作用,而且对我来说也没有任何意义。

我的应用程序运行在很多服务器上(30+),我从来没有为这些服务器添加任何configuration。 在.NET 2.0或.NET 4.0下运行的应用程序的版本。

对我来说,解决scheme是重新注册IIS对IIS。

我用下面的命令行来实现这个…

 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -i 

确保你使用正确的方法:Post / Get,正确的内容types和正确的参数(数据)。

 $.ajax({ type: "POST", url: "/ajax.asmx/GetNews", data: "{Lang:'tr'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { generateNews(msg); } }) 

高超。

案例2 – 在同样的问题可以出现)在我的情况下,问题是由于以下行:

 <webServices> <protocols> <remove name="Documentation"/> </protocols> </webServices> 

它在服务器上运行良好,因为直接调用web服务函数 – 但是如果在debugging环境中直接从.Net运行服务,并且想要手动testing运行函数,将会失败。

对于logging,当我将旧应用程序从一台服务器移动到另一台服务器时,出现此错误。 我向<add name="HttpGet"/> <add name="HttpPost"/>添加了<add name="HttpGet"/> <add name="HttpPost"/>元素,该错误更改为:

 System.IndexOutOfRangeException: Index was outside the bounds of the array. at BitMeter2.DataBuffer.incrementCurrent(Int64 val) at BitMeter2.DataBuffer.WindOn(Int64 count, Int64 amount) at BitMeter2.DataHistory.windOnBuffer(DataBuffer buffer, Int64 totalAmount, Int32 increments) at BitMeter2.DataHistory.NewData(Int64 downloadValue, Int64 uploadValue) at BitMeter2.frmMain.tickProcessing(Boolean fromTimerEvent) 

为了解决这个错误,我不得不将ScriptHandlerFactory行添加到web.config中:

  <system.webServer> <handlers> <remove name="ScriptHandlerFactory" /> <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </handlers> </system.webServer> 

为什么它在一台Web服务器上没有这些行,而不是其他的我不知道。

我在本地主机上开发时没有这个问题。 但是,一旦我发布到Web服务器,Web服务正在返回一个空的(空白)结果,我看到我的日志中的错误。

我通过设置我的ajax contentType来修复它:

 "application/json; charset=utf-8" 

并使用:

 JSON.stringify() 

在我发布的对象上。

 var postData = {data: myData}; $.ajax({ type: "POST", url: "../MyService.asmx/MyMethod", data: JSON.stringify(postData), contentType: "application/json; charset=utf-8", success: function (data) { console.log(data); }, dataType: "json" }); 

在html中,你必须用类似GET的forms来包含调用

 <a href="/service/servicename.asmx/FunctionName/parameter=SomeValue">label</a> 

您也可以使用POST作为Web服务的位置,并通过input标记input参数。

还有SOAP和代理类。

在我的情况下,我有一个导致此exception的函数的重载,一旦我改变了我的第二个函数的名称,它运行正常,猜测Web服务器不支持函数重载

我使用下面的代码行来解决这个问题。 在web.config文件中写入以下代码

 <configuration> <system.web.extensions> <scripting> <webServices> <jsonSerialization maxJsonLength="50000000"/> </webServices> </scripting> </system.web.extensions> </configuration> 

我也用apache mod-mono得到这个错误。 它看起来像web服务的文档页面尚未在linux中实现。 但Web服务正在工作,尽pipe这个错误。 您应该通过在URL末尾添加?WSDL来查看它,即http://localhost/WebService1.asmx?WSDL

确保您禁用自定义错误。 这可以掩盖您的代码中的原始问题:

更改

 <customErrors defaultRedirect="~/Error" mode="On"> 

 <customErrors defaultRedirect="~/Error" mode="Off"> 

一个需要ContextKey的WebMethod,

 [WebMethod] public string[] GetValues(string prefixText, int count, string contextKey) 

当这个键没有设置,得到exception。

通过分配AutoCompleteExtender的键来修复它。

 ac.ContextKey = "myKey";