Call ASP.NET web service via GET method

We know HTTP GET method is used to retrieve information from the server. Some times we need to use this GET method in ASP.NET web service. This article describes how to make an ASP.NET web service to accept GET requests? Or Enable ASP.NET ASMX web service for HTTP GET requests.

Normally we call the method of web service by using POST request. But sometimes we need to use GET method. We can do this by setting the UseHttpGet property to true. The C# code is: [ScriptMethod(UseHttpGet = true)]

At first we need to add the following namespace:

using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

A sample web service method is like this:

[WebMethod]
public string MyMethod(string MyString)
{
    return "You entered " + MyString;
}

Now use [ScriptMethod(UseHttpGet = true)] over your method. Final code is like:

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string MyMethod(string MyString)
{
    return "You entered " + MyString;
}

We may need to tell our application to accept GET requests in the web.config file.

 
  <system.web>
  ...
  <webServices>
    <protocols>
      <add name="HttpGet"/>
    </protocols>
  </webServices>
  ...
</system.web>

Now we can call the service from the URL:
http://localhost:5045:444/MyService.asmx/ MyMethod?MyString =London

Remember that GET method is unsecured. Because, query string is visible to the user. So use this method carefully.

Comments

  1. By SimpleAPI

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *