Web Services Using Query String Parameter .Net Framework In C#

Web Service, in general, is just a reusable function that can be called from any program over HTTP.
 
Script to create Web Service in Visual Studio.
  1. [WebMethod]  
  2. [ScriptMethod(UseHttpGet = true)]  
  3. public int Add(int a, int b) {  
  4.     return (a + b);  
  5. }  
ScriptMethod is used for getting parameter's value through Query string and also, to use other AJAX request mechanism [mainly Get operation]. It helps in providing resopnse in XML formatted manner.
In order to use query string parameter, there are a few modifications needed in web.config file inside system.web tag.

  1. <webServices>  
  2.     <protocols>  
  3.         <add name="HttpGet" /> </protocols>  
  4. </webServices> 
Once it is setup, to test the Web Service using browser console itself, run the application by hitting F5 and open a page browser where jQuery is included. Click on F12 and navigate to console module and run the below code.
  1. $.ajax({  
  2.     url: "http://localhost:XXXXX/ArthimeticService.asmx/Add?a=6&b=8",  
  3.     datatype: "html"// the data response it can be xml,json  
  4.     contentType: "text/plain"// the data format how we are sending the request  
  5. }).done(function(data) {  
  6.     var t = data.childNodes;  
  7.     console.log($(t).text())  
  8. });  


Both, dataType and contentType are not mandatory. These are used just to check different responses. We will get the response in the data object.

Note

Only after it is published, it will be available on internet, else it can be checked on localhost system only.
Ebook Download
View all
Learn
View all