帮助我理解网络方法?

发布于 2024-08-07 01:13:14 字数 192 浏览 4 评论 0原文

我在标有 webmethod 和 scriptmethod 标签的页面上有一个方法。

该方法将对象集合作为 JSON 数据返回到 jquery 函数,没有任何麻烦,而且不需要我手动序列化它。

我现在尝试使用 HTTPHandler 重新创建相同的方法,并且想知道为什么我现在必须手动序列化数据。

是什么让网络方法与众不同?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女情怀诗 2024-08-14 01:13:14

因为 HTTP 处理程序(某种意义上)位于 ASP WebForms 堆栈之上,所以您完全负责该处理程序的工作和输出。

您可以利用(几乎)任何您能在 .NET 框架中获得的东西,但可以肯定的是,HTTPHandler 比 ASP.NET 提供的现成解决方案需要更多工作。

ASP.NET 页面处理程序只是一个
处理程序的类型。 ASP.NET 附带
其他几个内置处理程序,例如
作为 .asmx 的 Web 服务处理程序
文件。

您可以创建自定义 HTTP 处理程序
当你想要特殊处理时
您可以使用文件名来识别
应用程序中的扩展

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

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

懵少女 2024-08-14 01:13:14

Web 方法为您提供了 C# 类和 Js 文件之间的连接。如今,使用 Json 是以智能格式获取 js 函数或 js 文件中任何位置的返回消息的最佳方法。

问候

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

不可一世的女人 2024-08-14 01:13:14

对于较少的工作:
将您的方法移至 ASMX(Web 服务):
您将受益于 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;
  }

}

在客户端:

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);
   }
});

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);
   }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文