Request format is unrecognized for URL unexpectedly ending in
Asked Answered
S

16

302

When consuming a WebService, I got the following error:

Request format is unrecognized for URL unexpectedly ending in /myMethodName

How can this be solved?

Seymourseys answered 18/3, 2009 at 8:5 Comment(2)
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.".Pythoness
And the Chinese translation: "無法辨認要求格式,因為 URL 未預期地以 /myMethodName 結束。"Agricola
S
556

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

Seymourseys answered 18/3, 2009 at 8:5 Comment(20)
i THINK all you need to do is switch <system.web> to <system.webserver>Seymourseys
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.Thedathedric
It just prevented me from seeing the error. Now what I can see is just a blank page.Evolute
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.Wethington
And what about if this error is thrown without any regularity, just sometimes? Are such calls dependent on some client/browser configuration!?Strangulate
This did not work for me, it required ASP.NET to be re-registered against IIS. See my answer for more informationRupture
After more than 5 years of using WCF, I almost completely forgot about this. Thx. +1Cabe
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.Cyprinodont
Why isn't the answer with 300+ upvotes displayed at the top I wonder. THANK YOU THIS WORKED thank you for the education.Accommodative
<add name="Documentation"/> in case you are accessing files like .asmxEmmetropia
Great solution you saved my ass. I was able to meet the deadline because of that. Million ThanksBerthold
Was facing this issue in my Web App on Azure App Service (on my azure website to be specific). This worked for me! Hurrahhh!Committeewoman
@SaurabhRai me too. What did this do? The links provided in the answer are broken.Ballot
@Ballot It's a mystery for me too. The closest I could find with a quick search is #619400 and yeah the links are brokenCommitteewoman
@romanm Can you please update the reference links. They are broken. Thanks a ton!Committeewoman
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!Darreldarrell
nice this fix my 3 day problem finally this fix my issue thank youManagua
This just brought me back a new error of $metadata Web Service method name is not valid.Saks
Now it gives an other error: "System.InvalidOperationException: Missing parameter sSerializedXML."Meggs
This worked for me, more than once.Marquee
R
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
Rupture answered 19/11, 2013 at 11:31 Comment(1)
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.Williamson
P
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); }
})
Pothead answered 26/1, 2011 at 18:28 Comment(3)
my parameter value passing got issue due to datatype, and missing values, which end up in 500 error. now it is solved.Flotation
Add content-type: application/json solved this problem for me too.Compagnie
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.Underhung
E
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.

Ernst answered 5/2, 2013 at 6:26 Comment(1)
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.Wimbush
C
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.

Camaraderie answered 1/4, 2014 at 11:22 Comment(1)
The "handlers"-configuration did the trick for me. Although I had them on my dev machine but not on the production server.Chitin
V
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>
Vadim answered 31/7, 2018 at 20:53 Comment(0)
E
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>
Eldridge answered 26/4, 2016 at 10:39 Comment(0)
R
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"
            });
Rosati answered 26/10, 2016 at 18:8 Comment(0)
P
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

Potash answered 18/12, 2016 at 22:5 Comment(0)
E
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.

Eskil answered 4/6, 2012 at 14:18 Comment(0)
N
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

Nikolaus answered 13/4, 2015 at 5:25 Comment(0)
P
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";
Puma answered 28/9, 2015 at 15:51 Comment(1)
Thanks, I was copying some code and had missed these extra partsCatching
W
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();
        }
    }
Wiles answered 12/4, 2018 at 23:38 Comment(0)
B
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>
Bioastronautics answered 31/5, 2019 at 15:40 Comment(0)
H
0

For anyone still having problems with this in 2024 using ASP.NET 4.8, below should work. The error happens because some of the HTTP verbs are automatically disabled for security reasons.

Edit your web.config by adding the following (remove any verbs you do not need):

    <system.web> 
      <webServices>
        <protocols>
            <add name="HttpSoap"/>
            <add name="HttpPost"/>
            <add name="HttpGet"/> 
            <add name="HttpPostLocalhost"/>
            <add name="Documentation"/>  <!-- Documentation pages -->
        </protocols>
    </webServices>
    ...
    </system.web>

My situation:

I had the problem when working with old .NET .asmx webservices. In the past, those webservices were called with AjaxControlToolkit but internal company policies requested standardization, so now the calls are done with javascript fetch API instead, which works.

jQuery Ajax failed too.

With above changes to web.config, the legacy services can now be called again. It is becoming expensive to maintain old legacy code.

Hendrika answered 1/2 at 14:44 Comment(0)
P
-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">
Phemia answered 15/5, 2015 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.