views:

361

answers:

4

I have a method on a page marked with the webmethod and scriptmethod tags..

The method returns a collection of objects to a jquery function as JSON data with no hassles and without me having to manually serialize it.

I am now trying to recreate that same method using a HTTPHandler and was wondering why i have to now manually serialize the data.

What makes the webmethod different?

A: 

Web method provides you a connection between your c# class and Js file. Nowadays Using Json is a best way to get the return message in a smart format for a js function or anywhere in js file.

Regards

blgnklc
+1  A: 

Because an HTTP handler (kind of) sits above the ASP WebForms Stack, you are totally responsible for the workings and output of the handler.

You can utilise (almost) anything you can get your hands on within the .NET framework, but for sure, an HTTPHandler will be more work than an off-the-shelf solution provided by ASP.NET.

The ASP.NET page handler is only one type of handler. ASP.NET comes with several other built-in handlers such as the Web service handler for .asmx files.

You can create custom HTTP handlers when you want special handling that you can identify using file name extensions in your application

See http://msdn.microsoft.com/en-us/library/ms227675(VS.85).aspx

Greg B
A: 

For lesser work: Move your method to an ASMX (Web Service): You will benefit the built-in serialization provided by the ScriptService:

namespace WS{

  [System.web.Script.Services.ScriptService()] 
  [System.Web.Services.WebService(Namespace:="http://tempuri.org/")]
  public class WebService1 : System.Web.Services.WebService
  {
      [WebMethod]  
      public Person GetDummyPerson()
      {
          Person p = new Person();
          p.Name = "John Wayne";
          p.Age = 20;
      }

      [WebMethod] 
      public IList GetPersonsByAge(int age)
      {
          //do actual data retrieval
          List result = new List();
          result.add(new Person());
          result.add(new Person());
          return result; 
      }
  }

  class Person 
  {
      String Name;
      int Age;
  }

}

On the client side:

WS.GetDummyPerson(function(p){
    alert(p.Name + "-->" + p.Age);
});

WS.GetPersonsByAge(10,function(list){
   for(var i=0;i<list.length;i++)
   {
      document.write(list[i].Name + "==>" + list[i].Age);
   }
});
jerjer
A: 

error on (Namespace:= and error on GetDummyPerson help? please!

paul allen