Wednesday, June 27, 2012

Request format is unrecognized for URL unexpectedly ending in methodname

This is the exception I am getting when I try to access web services from my web site. I just created a web service and tried to access the service from the browser using the get request. But I got the invalid operation exception. See complete exception details below:
System.InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/MethodName'
So, Before we will go to know the solution for it, I want to explain the reason behind this.
A .NET web service supports three protocols named HTTPGET, HTTPPOST and SOAP. When .NET released I mean, ASP.NET v1.0 all these three protocols enabled by default. So, any requests which made from the browser which belongs to a web service will be responded without any issues. But when come to ASP.NET 1.1 version the protocols HTTPGET and HTTPPOST are disabled by default. The only reason is SECURITY issue. So until you explicitly enable them no web service request will be processed if you call them in the format domain/webservice.asmx/methodname [Get/Post].
But, where as if you call direct web service from browser using SOAP request that will work and will give result what you need. But, if you make any GET or POST request then only the problem will come. 
RESOLUTION:
To enable these protocols in the application level simply we need to change the application config file. Web.config. Add below lines to the web.config to enable these protocols.
 
<configuration>
     <system.web> 
    <webservices>      
  <protocols>       
  <add name="HttpGet"></add>  
           <add name="HttpPost"></add>     
     </protocols>     
 </webservices>     
 </system.web>
  </configuration>  
So, this webservices tag will go inside the <system.web> namespace. With the above lines, the protocols enabled and they listen the request and respond accordingly for that application. Sometimes you may need to enable these protocols in system level. I mean to all applications in the server should have these protocols enabled. Then below is the solution for it.
Find machine.config file and open it in nice editor like visual studio. Before do any changes please take a backup of the file. Now find the protocols section and add the below lines if they are not already there.
 
<protocols>   
<add name="HttpSoap"></add> 
 <add name="HttpPost"></add>   
<add name="HttpGet"></add> 
 </protocols> 

No comments:

Post a Comment