在ASP.NET Atlas中调用Web Service
2008-11-20 11:30
621 查看
Atlas Framework中包含了对AJAX调用的封装,让您可以很方便的在客户端通过JavaScript调用服务器端方法。在本篇文章中,我将解释一下如何使用Atlas调用服务器端Web Service。
使用Atlas,我们只需要如下步骤即可调用服务器端Web Service:
在Web Service的方法上加上[WebMethod]属性。
在ASPX页面上的ScriptManager中添加对这个Web Service的引用。
只需以上两步,Atlas会在运行时为您生成相应的mash up,让您可在客户端JavaScript中通过WebServiceClassName.ServiceMethodName()调用该方法。
让我们先来看一个最简单的例子,调用服务器端Web Service得到两个数的和:
首先建立一个Web Service:SimpleWebService.asmx,并在其中添加一个Service Method,不要忘记标记为[WebMethod]哦: [WebMethod]
public int AddInt(int int1, int int2)
{
return int1 + int2;
}
然后再ASPX页面上的ScriptManager中添加对该Web Service的引用:
上面的例子仅仅传递简单类型,然而在现实世界中,我们经常会需要传递一些复杂的类型,让我们看一个传递复杂类型的例子:
本例子同样是一个加法,不过这回操作的类型是复数。让我们先来看看C#中我们的复数的定义(作为示例,这里尽可能的简化)。注意我们应该提供自定义的复杂类型一个无参的构造函数,以便于Atlas自动在C#类型和JavaScript类型中转换:
public class ComplexNumber
{
private int real;
public int Real
{
get { return real; }
set { real = value; }
}
private int imag;
public int Imag
{
get { return imag; }
set { imag = value; }
}
public ComplexNumber(int real, int imag)
{
this.real = real;
this.imag = imag;
}
public ComplexNumber()
{
}
}
使用Atlas,我们只需要如下步骤即可调用服务器端Web Service:
在Web Service的方法上加上[WebMethod]属性。
在ASPX页面上的ScriptManager中添加对这个Web Service的引用。
只需以上两步,Atlas会在运行时为您生成相应的mash up,让您可在客户端JavaScript中通过WebServiceClassName.ServiceMethodName()调用该方法。
让我们先来看一个最简单的例子,调用服务器端Web Service得到两个数的和:
首先建立一个Web Service:SimpleWebService.asmx,并在其中添加一个Service Method,不要忘记标记为[WebMethod]哦: [WebMethod]
public int AddInt(int int1, int int2)
{
return int1 + int2;
}
然后再ASPX页面上的ScriptManager中添加对该Web Service的引用:
上面的例子仅仅传递简单类型,然而在现实世界中,我们经常会需要传递一些复杂的类型,让我们看一个传递复杂类型的例子:
本例子同样是一个加法,不过这回操作的类型是复数。让我们先来看看C#中我们的复数的定义(作为示例,这里尽可能的简化)。注意我们应该提供自定义的复杂类型一个无参的构造函数,以便于Atlas自动在C#类型和JavaScript类型中转换:
public class ComplexNumber
{
private int real;
public int Real
{
get { return real; }
set { real = value; }
}
private int imag;
public int Imag
{
get { return imag; }
set { imag = value; }
}
public ComplexNumber(int real, int imag)
{
this.real = real;
this.imag = imag;
}
public ComplexNumber()
{
}
}
相关文章推荐
- 在asp.net Atlas中调用 Web Service时无法找到自定义的Web Service对象的可能原因
- 在ASP.NET Atlas中调用Web Service——介绍及简单应用
- 在ASP.NET Atlas中调用Web Service——批量调用以提高效率
- 在ASP.NET Atlas中调用Web Service——创建Mashup调用远端Web Service(Google Search实例)
- 在ASP.NET Atlas中调用Web Service——直接在ASPX页面上暴露Web Method
- 在ASP.NET Atlas中调用Web Service——创建Mashup调用远端Web Service(Yahoo!天气实例)
- 在ASP.NET Atlas中调用Web Service——应用Cache减小服务器开销
- 在ASP.NET Atlas中调用Web Service——处理错误,超时以及响应用户的取消操作
- 在ASP.NET Atlas中调用Web Service——创建Mashup调用远端Web Service(基础知识以及简单示例)
- 在ASP.NET Atlas中调用Web Service——创建Mashup调用远端Web Service(基础知识以及简单示例)
- 在ASP.NET Atlas中调用Web Service——创建Mashup调用远端Web Service(基础知识以及简单示例)
- 在ASP.NET Atlas中调用Web Service——处理错误,超时以及响应用户的取消操作
- 在ASP.NET Atlas中调用Web Service
- 在ASP.NET Atlas中调用Web Service——在页面加载时调用Web Service
- 在ASP.NET Atlas中调用Web Service
- 在ASP.NET Atlas中调用Web Service——处理错误,超时以及响应用户的取消操作
- 在ASP.NET Atlas中调用Web Service——创建Mashup调用远端Web Service(基础知识以及简单示例)
- 在ASP.NET Atlas中调用Web Service——介绍及简单应用
- ASP.NET里调用Web Service
- asp.net调用远程Web Service的二种方法