September 20, 2024
application-programming-interface-api

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.

Rashedul Alam

I am a software engineer/architect, technology enthusiast, technology coach, blogger, travel photographer. I like to share my knowledge and technical stuff with others.

View all posts by Rashedul Alam →

5 thoughts on “Call ASP.NET web service via GET method

  1. Made changes in .cs and web.config file. but it is still showing POST method while Invoke
    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public string GetDistance(string sOrigin, string sDestination)
    {
    string sDistanceKMS = DistanceMatrixRequest(sOrigin, sDestination);

    return sDistanceKMS;
    }

  2. I read that Post and got it fine and informative. Please share more like that…Great Article it its really informative. Its was really valuable. Thanks a lot.

Leave a Reply

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