300

When consuming a WebService, I got the following error:

Request format is unrecognized for URL unexpectedly ending in /myMethodName

How can this be solved?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
roman m
  • 26,012
  • 31
  • 101
  • 133
  • 5
    To make it easier for Google, the German translation of the error message reads "**Unbekanntes Anforderungsformat für eine URL, die unerwartet mit '/_myMethodName' endet.**". – Uwe Keim Mar 27 '17 at 06:54
  • And the Chinese translation: "**無法辨認要求格式,因為 URL 未預期地以 /myMethodName 結束。**" – Ignatius Aug 30 '17 at 10:54

15 Answers15

548

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

roman m
  • 26,012
  • 31
  • 101
  • 133
  • 3
    i THINK all you need to do is switch to – roman m Apr 19 '10 at 18:56
  • i kept it as is and for now the error seems to have gone away. if i see the error again i'll move the webservices configs into the webserver section. – Daniel Brink Apr 20 '10 at 08:08
  • It just prevented me from seeing the error. Now what I can see is just a blank page. – Lance Aug 22 '11 at 09:23
  • In my case, adding the HttpGet/HttpPost protocol names had no effect, but it did point me in the direction that I had a problem with my web.config. I had recently changed the name of a database connection string; precisely what I did wrong still eludes me, but reverting back to the original connection string name resolved the problem. There were no compilation errors, but something was out of sync between my web.config and dataset xsd. – Bill B Jan 27 '13 at 19:05
  • 1
    And what about if this error is thrown without any regularity, just sometimes? Are such calls dependent on some client/browser configuration!? – Vladislav Sep 20 '13 at 11:46
  • This did not work for me, it required ASP.NET to be re-registered against IIS. See my answer for more information – freefaller Nov 19 '13 at 11:26
  • After more than 5 years of using WCF, I almost completely forgot about this. Thx. +1 – cstick Jan 22 '14 at 12:23
  • 2
    On a Win2012srv with IIS8 it was needed. On a Win8 with IIS8 it was not needed. No other discrepancies in the configuration that I know of. – LosManos Jun 13 '14 at 12:57
  • Why isn't the answer with 300+ upvotes displayed at the top I wonder. THANK YOU THIS WORKED thank you for the education. – taki Martillo Jul 31 '15 at 19:21
  • 1
    in case you are accessing files like .asmx – sandeep talabathula Jun 20 '16 at 11:58
  • Great solution you saved my ass. I was able to meet the deadline because of that. Million Thanks – BILAL AHMAD Oct 17 '17 at 17:26
  • Was facing this issue in my Web App on Azure App Service (on my azure website to be specific). This worked for me! Hurrahhh! – Saurabh Rai Apr 01 '18 at 18:33
  • 1
    @SaurabhRai me too. What did this do? The links provided in the answer are broken. – Rod Apr 04 '18 at 20:44
  • @Rod It's a mystery for me too. The closest I could find with a quick search is https://stackoverflow.com/questions/618900/enable-asp-net-asmx-web-service-for-http-post-get-requests and yeah the links are broken – Saurabh Rai Apr 05 '18 at 08:16
  • @romanm Can you please update the reference links. They are broken. Thanks a ton! – Saurabh Rai Apr 05 '18 at 08:18
  • This change to the web config worked for me! I'm trying to fix a legacy system issue. Of course I have other issues to fix, but it executed the Web Service request! – justdan23 Nov 05 '18 at 23:50
  • nice this fix my 3 day problem finally this fix my issue thank you – Cesar Vega Jan 04 '19 at 17:12
  • This just brought me back a new error of `$metadata Web Service method name is not valid`. – Matt Arnold Feb 09 '21 at 15:52
  • Now it gives an other error: "System.InvalidOperationException: Missing parameter sSerializedXML." – Jánosi Zoltán János Jul 27 '22 at 13:47
18

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
Community
  • 1
  • 1
freefaller
  • 19,368
  • 7
  • 57
  • 87
  • This fixed my issue. I was getting the same error as OP; on what had been a previously working site. Turns out someone enabled .NET 3.5 through windows features (for unrelated reasons) which broke my site. `aspnet_regiis -i` fixed it though. – Nate Jul 15 '15 at 22:14
17

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); }
})
HasanG
  • 12,734
  • 29
  • 100
  • 154
  • 1
    my parameter value passing got issue due to datatype, and missing values, which end up in 500 error. now it is solved. – Pranesh Janarthanan Feb 16 '18 at 07:24
  • Add `content-type: application/json` solved this problem for me too. – Delphi.Boy Apr 27 '20 at 19:46
  • As it's related to Post/Get I'll add it here. I found Bingbot was crawling my ajax urls meaning it was doing GET requests on POST webmethods and that was why I was seeing errors. – Ben Close Jul 12 '23 at 11:01
15

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.

Kalpesh Popat
  • 1,416
  • 14
  • 12
  • Added that logic to the web.config to prevent the definition from displaying when browsing to the .asmx service. Apparently that broke ActiveReports. Glad to know it is likely a symptom of testing local and it will work on the server. Thanks. – Jacob Barnes Aug 21 '17 at 16:29
3

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.

Sprintstar
  • 7,938
  • 5
  • 38
  • 51
  • The "handlers"-configuration did the trick for me. Although I had them on my dev machine but not on the production server. – Björn Oct 18 '21 at 07:10
2

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>
Ruby Kousinovali
  • 337
  • 2
  • 20
1

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>
1

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"
            });
Jason Williams
  • 2,740
  • 28
  • 36
1

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

l0pan
  • 476
  • 7
  • 11
0

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.

Wilk
  • 7,873
  • 9
  • 46
  • 70
Dave
  • 169
  • 9
0

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

Amir Shrestha
  • 451
  • 5
  • 11
0

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";
Rm558
  • 4,621
  • 3
  • 38
  • 43
0

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();
        }
    }
cockypup
  • 1,013
  • 1
  • 10
  • 24
0

I was getting this error until I added (as shown in the code below) $.holdReady(true) at the beginning of my web service call and $.holdReady(false) after it ends. This is jQuery thing to suspend the ready state of the page so any script within document.ready function would be waiting for this (among other possible but unknown to me things).

<span class="AjaxPlaceHolder"></span>
<script type="text/javascript">
$.holdReady(true);
function GetHTML(source, section){
    var divToBeWorkedOn = ".AjaxPlaceHolder";
    var webMethod = "../MyService.asmx/MyMethod";
    var parameters = "{'source':'" + source + "','section':'" + section + "'}";

    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        xhrFields: {
            withCredentials: false
        },
        crossDomain: true,
        success: function(data) {
            $.holdReady(false);
            var myData = data.d;
            if (myData != null) {
                $(divToBeWorkedOn).prepend(myData.html);
            }
        },
        error: function(e){
            $.holdReady(false);
            $(divToBeWorkedOn).html("Unavailable");
        }
    });
}
GetHTML("external", "Staff Directory");
</script>
estinamir
  • 435
  • 5
  • 11
-1

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">