Help me understand web methods?

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

The method returns a collection of jquery function objects as JSON data without any problems and without the need to manually serialize it.

Now I'm trying to recreate the same method using the HTTPHandler, and wondered why I should now manually serialize the data.

What is the difference between the web method?

+3
source share
3 answers

Since the HTTP handler (view) is located above the ASP WebForms stack, you are solely responsible for the operation and output of the handler.

() , .NET, , , HTTPHandler , , ASP.NET.

ASP.NET - . ASP.NET , Web- .asmx .

HTTP ,

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

+1

- # Js. Json - js- - js .

0

For less work: Move your method to ASMX (web service): You will win the built-in serialization provided by 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);
   }
});
0
source

Source: https://habr.com/ru/post/1719379/


All Articles