Request format is unrecognized for URL unexpectedl

2019-01-01 08:22发布

问题:

This is not a question - posting it here for reference:

When consuming a WebService, I got the following error:

Request format is unrecognized for URL unexpectedly ending in /myMethodName

回答1:

Found a solution on this website

All you need is to add the following to your web.config

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

More info from Microsoft



回答2:

Despite 90% of all the information I found (while trying to find a solution to this error) telling me to add the HttpGet and HttpPost to the configuration, that did not work for me... and didn\'t make sense to me anyway.

My application is running on lots of servers (30+) and I\'ve never had to add this configuration for any of them. Either the version of the application running under .NET 2.0 or .NET 4.0.

The solution for me was to re-register ASP.NET against IIS.

I used the following command line to achieve this...

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


回答3:

Make sure you\'re using right method: Post/Get, right content type and right parameters (data).

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


回答4:

Superb.

Case 2 - where the same issue can arrise) in my case the problem was due to the following line:

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

It works well in server as calls are made directly to the webservice function - however will fail if you run the service directly from .Net in the debug environment and want to test running the function manually.



回答5:

For the record I was getting this error when I moved an old app from one server to another. I added the <add name=\"HttpGet\"/> <add name=\"HttpPost\"/> elements to the web.config, which changed the error to:

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)

In order to fix this error I had to add the ScriptHandlerFactory lines to 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>

Why it worked without these lines on one web server and not the other I don\'t know.



回答6:

I use following line of code to fix this problem. Write the following code in web.config file

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


回答7:

In html you have to enclose the call in a a form with a GET with something like

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

You can also use a POST with the action being the location of the web service and input the parameter via an input tag.

There are also SOAP and proxy classes.



回答8:

In my case i had an overload of function that was causing this Exception, once i changed the name of my second function it ran ok, guess web server doesnot support function overloading



回答9:

I did not have the issue when developing in localhost. However, once I published to a web server, the webservice was returning an empty (blank) result and I was seeing the error in my logs.

I fixed it by setting my ajax contentType to :

\"application/json; charset=utf-8\"

and using :

JSON.stringify()

on the object I was posting.

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\"
            });


回答10:

I also got this error with apache mod-mono. It looks like the documentation page for webservice is not implemented yet in linux. But the webservice is working despite this error. You should see it by adding ?WSDL at the end of url, i.e http://localhost/WebService1.asmx?WSDL



回答11:

In our case the problem was caused by the web service being called using the OPTIONS request method (instead of GET or POST).

We still don\'t know why the problem suddenly appeared. The web service had been running for 5 years perfectly well over both HTTP and HTTPS. We are the only ones that consume the web service and it is always using POST.

Recently we decided to make the site that host the web service SSL only. We added rewrite rules to the Web.config to convert anything HTTP into HTTPS, deployed, and immediately started getting, on top of the regular GET and POST requests, OPTIONS requests. The OPTIONS requests caused the error discussed on this post.

The rest of the application worked perfectly well. But we kept getting hundreds of error reports due to this problem.

There are several posts (e.g. this one) discussing how to handle the OPTIONS method. We went for handling the OPTIONS request directly in the Global.asax. This made the problem dissapear.

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        var req = HttpContext.Current.Request;
        var resp = HttpContext.Current.Response;

        if (req.HttpMethod == \"OPTIONS\")
        {
            //These headers are handling the \"pre-flight\" OPTIONS call sent by the browser
            resp.AddHeader(\"Access-Control-Allow-Methods\", \"GET, POST\");
            resp.AddHeader(\"Access-Control-Allow-Headers\", \"Origin, Content-Type, Accept, SOAPAction\");
            resp.AddHeader(\"Access-Control-Max-Age\", \"1728000\");
            resp.End();
        }
    }


回答12:

In my case the error happened when i move from my local PC Windows 10 to a dedicated server with Windows 2012. The solution for was to add to the web.config the following lines

<webServices>
        <protocols>
               <add name=\"Documentation\"/>
        </protocols>
</webServices>


回答13:

Make sure you disable custom errors. This can mask the original problem in your code:

change

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

to

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


回答14:

a WebMethod which requires a ContextKey,

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

when this key is not set, got the exception.

Fixing it by assigning AutoCompleteExtender\'s key.

ac.ContextKey = \"myKey\";