webservices想将二维数组作为传递参数该怎么办?

lihuangsuo 2009-03-17 01:25:34
webservices怎么不能传二维数组啊?
要想传递该怎么做,有什么变通的方案么。
俺是新手,最好给代码,调试通过即给分.谢谢了....
...全文
634 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
liqianfu0214 2009-04-03
  • 打赏
  • 举报
回复
1 /// <summary>
2 /// 返回字符串
3 /// </summary>
4 /// <returns> </returns>
5 [WebMethod]
6 public string HelloWorld()
7 {
8 return "Hello World";
9 }
10
11 /// <summary>
12 /// 返回一个简单对象
13 /// </summary>
14 /// <returns> </returns>
15 [WebMethod]
16 public Book GetBook()
17 {
18 return new Book
19 {
20 Id = 1,
21 Name = "三国演义",
22 Author = "罗贯中",
23 Price = 100
24 };
25 }
lxf2000104 2009-03-19
  • 打赏
  • 举报
回复
可以传实体类啊(序列化)
the_pain 2009-03-19
  • 打赏
  • 举报
回复
up
lihuangsuo 2009-03-19
  • 打赏
  • 举报
回复
Help!!!
Help me.....
lihuangsuo 2009-03-19
  • 打赏
  • 举报
回复
唉,妈的,遇到瓶颈了,这几天搞这个问题搞得头疼死了,程序员真不是人干的...
lihuangsuo 2009-03-19
  • 打赏
  • 举报
回复
能够说具体点吗?
zzxap 2009-03-19
  • 打赏
  • 举报
回复
把一个数据集传过来,
lihuangsuo 2009-03-19
  • 打赏
  • 举报
回复
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;


/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{

public WebService()
{

//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}

[WebMethod]
public Employee GetEmployeeOne()
{
return new Employee("beniao", "boy", "22");
}

[WebMethod]
public Employee GetEmployeeTwo(Employee employee)
{
return new Employee(employee.Name, employee.Sex, employee.Age);
}


//[WebMethod]
// 这里该怎么定义接收二维数组的参数?????
//public Employee GetEmployee(string []arr)
//{
// return new Employee(arr[0] arr[0].Sex, arr[0].Age);
//}

}


lihuangsuo 2009-03-19
  • 打赏
  • 举报
回复
在网上找了个例子.
GetEmployeeOne()和GetEmployeeTwo()都可以返回
但二维数组不知道该怎么写?
default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>



<script type="text/javascript">
function GetEmployeeOne()
{
WebService.GetEmployeeOne(CallBack);
}

function GetEmployeeTwo()
{
//使用JSON进行传输
var employee = {"Name":"Yuang", "Sex":"girl", "Age":"21"};
WebService.GetEmployeeTwo(employee , CallBack);
}

function GetEmployee()
{
//使用JSON进行传输
var employee = [{"Name":"Yuang", "Sex":"girl", "Age":"21"},{"Name":"zz", "Sex":"boy", "Age":"88"}];
WebService.GetEmployee(employee , CallBack);
}

//回调函数
function CallBack(employee)
{
var obj=document.getElementById("Result");
obj.innerHTML=String.format("Name:{0} Sex:{1} Age:{2}",
employee.Name,
employee.Sex,
employee.Age);
}
</script>
</head>
<body>
<form id="form1" runat="server">

<div>
<asp:ScriptManager ID="ScriptManager2" runat="server">
<Services>
<asp:ServiceReference Path="WebService.asmx" InlineScript="true" />
</Services>
</asp:ScriptManager>

</div>
<input id="Button1" type="button" value="无参数交互" onclick="GetEmployeeOne();" />
<input id="Button2" type="button" value="带参数交互" onclick="GetEmployeeTwo();" />
<input id="Button3" type="button" value="测试" onclick="GetEmployee();" />
<br /><br />
<div id="Result" style="width:230px;height:30px;font-weight:bold;font-size:medium;background-color:#CCC;"></div>

</form>
</body>
</html>




Employee.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Class1 的摘要说明
/// </summary>

public class Employee
{
/**//// <summary>
/// 作为参数的类型一定要有默认的构造函数
/// </summary>
public Employee()
{
}

public Employee(string name, string sex, string age)
{
this.Name = name;
this.Sex = sex;
this.Age = age;
}

private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}

private string _sex;
public string Sex
{
get { return _sex; }
set { _sex = value; }
}

private string _age;
public string Age
{
get { return _age; }
set { _age = value; }
}
}
lihuangsuo 2009-03-19
  • 打赏
  • 举报
回复
听了一个前辈说可以用json传,我这几天看了下json的资料。可是在webservices里面该怎么定义这个参数呢?
这是js:

function GetEmployee()
{
//使用JSON进行传输
var employee = [{"Name":"Yuang", "Sex":"girl", "Age":"21"},{"Name":"zz", "Sex":"boy", "Age":"88"}];
WebService.GetEmployee(employee , CallBack);
}

//回调函数
function CallBack(employee)
{
var obj=document.getElementById("Result");
obj.innerHTML=String.format("Name:{0} Sex:{1} Age:{2}",
employee.Name,
employee.Sex,
employee.Age);
}


可在webservices里面该怎么接收这个二维数组?

[WebMethod]
public Employee GetEmployee(string []arr)
{
//return new Employee(arr[0], arr[0].Sex, arr[0].Age);
}



wonder888888 2009-03-19
  • 打赏
  • 举报
回复
1 /// <summary>
2 /// 返回字符串
3 /// </summary>
4 /// <returns></returns>
5 [WebMethod]
6 public string HelloWorld()
7 {
8 return "Hello World";
9 }
10
11 /// <summary>
12 /// 返回一个简单对象
13 /// </summary>
14 /// <returns></returns>
15 [WebMethod]
16 public Book GetBook()
17 {
18 return new Book
19 {
20 Id = 1,
21 Name = "三国演义",
22 Author = "罗贯中",
23 Price = 100
24 };
25 }


如上便是WebService方法定义和在Flex的客户端(mxml)通过<mx:WebService>标签来访问WebService的完整流程,下面我们来看看在Flex的客户端怎么去调用WebService所定义的方法:

1 <mx:Script>
2 <![CDATA[
3 import mx.controls.Alert;
4 import mx.rpc.events.FaultEvent;
5 import mx.rpc.events.ResultEvent;
6
7 /**
8 * 向WebService发起请求--调用HelloWorld方法,dataService为<mx:WebService>的id
9 * */
10 internal function onRequest():void
11 {
12 dataService.HelloWorld();
13 }
14
15 /**
16 * 请求成功处理返回结果
17 * */
18 internal function onSuccess(evt:ResultEvent):void
19 {
20 Alert.show(evt.result.toString());
21 }
22
23
24 /**
25 * 请求失败的处理函数
26 * */
27 internal function onFault(evt:FaultEvent):void
28 {
29 Alert.show("访问WebService失败!");
30 }
31 ]]>
32 </mx:Script>

通过上面的调用,就可以完成一个Flex和.NET WebService的交互。当然我们在Flash/Flex的客户端调用WebService也是可以传递参数的,如下WebService的WebMethod定义:

1 /// <summary>
2 /// 将传递进来的参数转化为大写字符返回
3 /// </summary>
4 /// <param name="value"></param>
5 /// <returns></returns>
6 [WebMethod]
7 public string ConvertToUpper(string value)
8 {
9 return value.ToUpper();
10 }

通过在<mx:WebService>标签下配置<mx:operation>执行该方法就可以访问了,如下:

1 <mx:operation name="ConvertToUpper" result="onSuccess(event)" fault="onFault(event)"/>


1 /**
2 * 向WebService发起请求
3 * */
4 internal function onRequest():void
5 {
6 //dataService.HelloWorld();
7 dataService.ConvertToUpper("abcdefg");
8 }

另外,我们还可以通过<mx:request>来传递参数,这里只需要知道<mx:request></mx:request>里的参数配置与WebService提供的WebMethod方法参数同名就OK。

回到前面看看WebService的方法定义,其中一个方法GetBook是返回的一个Book对象,如果是返回的对象我们在Flex的客户端怎么来获取这个对象的值呢?详细见如下代码示例:

1 internal function onObject():void
2 {
3 dataService.GetBook();
4 }
5
6 internal function onObjectSuccess(evt:ResultEvent):void
7 {
8 //直接通过事件的result属性得到返回值,然后直接访问属性便OK
9 Alert.show(evt.result.Name);
10 }
11
12 /**
13 * 请求失败的处理函数
14 * */
15 internal function onFault(evt:FaultEvent):void
16 {
17 Alert.show("访问WebService失败!");
18 }

如上便完成了服务端的WebService返回对象到客户端的调用。

wonder888888 2009-03-19
  • 打赏
  • 举报
回复
webservice是基于XML,soap,WSDL等一系列标准协议的,与平台无关正是其优点之一, 客户端只管调用服务端的webserivce服务就行,
gaozhigang 2009-03-17
  • 打赏
  • 举报
回复
你传个结构就可以了。很好用的
lude8880 2009-03-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sjt000 的回复:]
.net不支持多维数组。
将2维数组序列化为1维。 
收到后在将1维还原为2维。
[/Quote]
。。。
德仔 2009-03-17
  • 打赏
  • 举报
回复
整个类传过去吧

要么用split来分拆出来.
lihuangsuo 2009-03-17
  • 打赏
  • 举报
回复
up
lihuangsuo 2009-03-17
  • 打赏
  • 举报
回复
up
lihuangsuo 2009-03-17
  • 打赏
  • 举报
回复
麻烦能给个简单的代码么.谢谢啦....
wuyq11 2009-03-17
  • 打赏
  • 举报
回复
把数据写成XML,通过XML传递数据
sjt000 2009-03-17
  • 打赏
  • 举报
回复
.net不支持多维数组。
将2维数组序列化为1维。
收到后在将1维还原为2维。
加载更多回复(2)

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧