1

I am constantly finding a good guide about how to write a web service using .NET with Visual Studio 2010 so I can utilize it with my HTML based website using AJAX.

I know there was a way called the ASMX way but now it's more updated to ServiceHost so what I need is a simple guide which can drive me through how to create asp.net web service using ServiceHost object.

Sorry if it sounds ambiguous or childish.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Umair A.
  • 6,690
  • 20
  • 83
  • 130
  • possible duplicate of [Using JQuery to call a WebMethod](http://stackoverflow.com/questions/563133/using-jquery-to-call-a-webmethod) – John Saunders Feb 02 '11 at 07:00

1 Answers1

2

Place the ScriptManager control on your page and add a reference to your .asmx service:

<asp:ScriptManager ID="myManager" runat="server">
    <Services>
        <asp:ServiceReference Path="~/MyService.asmx" />
    </Services>
</asp:ScriptManager>

In the code-behind of your web-service declare you web method (notice the ScriptService attribute):

namespace MyServices
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    public class MyService : System.Web.Services.WebService
    {
        [WebMethod]
        public string SayHello(name)
        {
             return "Hello, " + name;
        }
    }
}

Now you can consume the web-service from the Javascript like the following:

function queryWebService() {
    MyServices.MyService.SayHello('Bobby', 
    function(result, userContext) {
        alert('Web-service response: ' + result);
    }, function(result) {
        alert('Error!');
    });
}

UPDATE

If you want to consume the web-service by simply sending an HTTP GET requests then you can do the following:

Decorate your web-method with a ScriptMethod attribute:

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string SayHello(name)
{
    return "Hello, " + name;
}

Notice the UseHttpGet property which is set to True. You probably also need to modify the web.config file to allow this kind of interaction:

<webServices>
    <protocols>
        <add name="HttpGet"/>
    </protocols>
</webServices>

Now you can make a simple HTTP GET request to your web-service like shown below (the example uses jQuery.ajax):

$.ajax({
    url: "/MyService.asmx/SayHello?name=Bobby",
    success: function(transport) {
        alert('Web-service response: ' + transport.responseText);
    }
});

Hope this will help you.

volpav
  • 5,090
  • 19
  • 27
  • although your guide is helpful but I am not looking for such solution. The service is hosted over the cloud and Javascript will call them from different clients so I need to call them via HTTP GET method. Can I do that? – Umair A. Feb 02 '11 at 13:57
  • Will your web page be from the same (exact) host that the service will be hosted? ie. www.se.com will call www.se.com/myservice.asmx? If not, then you're going to have XSS issues (cross site scripting) and will have to use one of the few techniques to get around that. – Sean Feb 02 '11 at 14:12
  • No XSS issue because I am calling it from file:/// – Umair A. Feb 02 '11 at 14:21
  • @Volpav: I wonder why don't you prefer WCF way of doing this? – Umair A. Feb 02 '11 at 16:35
  • Because of the simplicity. No need to use a more complex technology if you don't see any obvious advantages of using it (I don't mean your case since I don't know all the aspects). [This](http://dotnetbyexample.blogspot.com/2008/02/calling-wcf-service-from-javascript.html) and [this](http://msdn.microsoft.com/en-us/library/bb514961.aspx) articles might help you to do it WCF way. – volpav Feb 02 '11 at 16:54