Execute Web Service via GET Method
Posted by Virendra Dugar on September 8, 2009
When we created any web service via Vistual Studio, we always get a default method named “Hello world”.
When you hit this URL in your browser, http://localhost/SampleWebService/Service.asmx, it shows list of all the web method. Click on Hello World and then Invoke. It gives following output:
I am assuming that you have hosted your web service in IIS.
<?xml version=”1.0″ encoding=”utf-8″ ?>
<string xmlns=”http://tempuri.org/“>Hello World</string>
Looks good. Now let’s go and modify our service.cs file and add a new method which takes one parameter.
public string HelloWorldWithParam(int a)
return "Hello World " + a.ToString();
Now, hit this URL http://localhost/SampleWebService/Service.asmx. It is showing both the methods.
Click on HelloWorldWithParam. It asks for parameter value. Without giving any value just click invoke.
It shows following error:
Above error is coming as method is expecting a value, and method tries to convert null value to string. If you refresh your browser then following error comes:
Now let’s provide the parameter value via URL only. Hit this URL, http://localhost/SampleWebService/Service.asmx/HelloWorldWithParam?a=1 , and then also you will receive the above error.
Reason: By default, Web service created via Visual Studio executes through HTTP Post method. When Post method is used, querystring is not visible. We need to configure our web service, so it works with HTTP GET and POST method. How can we do this?
Go to web.config and add this code in system.web section.
Good to go now. Hit this URL again and this time you will see “Hello World 1”.