在ASP.NET Atlas中调用Web Service——介绍及简单应用
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;
- }
[WebMethod] public int AddInt(int int1, int int2) { return int1 + int2; }
然后再ASPX页面上的ScriptManager中添加对该Web Service的引用:
- <atlas:ScriptManager ID="ScriptManager1" runat="server">
- <Services>
- <atlas:ServiceReference Path="SimpleWebService.asmx" />
- </Services>
- </atlas:ScriptManager>
<atlas:ScriptManager ID="ScriptManager1" runat="server"> <Services> <atlas:ServiceReference Path="SimpleWebService.asmx" /> </Services> </atlas:ScriptManager>
添加点HTML Code,让用户输入两个整数
- Pass simple type to web service - add the two integers:
- <input id="int1" type="text" value="1" size="3" />+
- <input id="int2" type="text" value="2" size="3" />=
- <input id="btnAddInt" type="button" value="?" onclick="return btnAddInt_onclick()" />
再书写一点JavaScript,当用户点击上面的按钮时,调用Web Method。这里要注意的是JavaScript中调用Web Method的格式:前面两个参数int1,int2分别对应着Web Service声明中的两个参数,后面一个参数onAddIntComplete表示方法成功返回时的Callback方法,也就是所谓AJAX中的A。同时需要注意的是$()方法,等同于document.getElementById()。
- function btnAddInt_onclick() {
- var int1 = $('int1').value;
- var int2 = $('int2').value;
- SimpleWebService.AddInt(int1, int2, onAddIntComplete);
- }
- function onAddIntComplete(result) {
- $('btnAddInt').value = result;
- }
上面的例子仅仅传递简单类型,然而在现实世界中,我们经常会需要传递一些复杂的类型,让我们看一个传递复杂类型的例子:
本例子同样是一个加法,不过这回操作的类型是复数。让我们先来看看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()
- {
- }
- }
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() { } }
然后是实现复数加法的Web Method,写在同一个Web Service中:
- [WebMethod]
- public ComplexNumber AddComplexNumber(ComplexNumber num1, ComplexNumber num2)
- {
- return new ComplexNumber(num1.Real + num2.Real, num1.Imag + num2.Imag);
- }
[WebMethod] public ComplexNumber AddComplexNumber(ComplexNumber num1, ComplexNumber num2) { return new ComplexNumber(num1.Real + num2.Real, num1.Imag + num2.Imag); }
相应的ASPX页面中也要添加一些HTML,让用户输入两个复数:
- Pass complex type to web service - add the two complex numbers:
- (<input id="cplx1r" type="text" value="1" size="3" />+
- <input id="cplx1i" type="text" value="2" size="3" />i) + (
- <input id="cplx2r" type="text" value="3" size="3" />+
- <input id="cplx2i" type="text" value="4" size="3" />i) =
- <input id="btnAddComplex" type="button" value="?" onclick="return btnAddComplex_onclick()" />
然后是相应的JavaScript,当用户点击上面的按钮时,执行这段JavaScript以调用Web Method。
- function btnAddComplex_onclick() {
- var cplx1 = {Real: $('cplx1r').value, Imag: $('cplx1i').value};
- var cplx2 = {Real: $('cplx2r').value, Imag: $('cplx2i').value};
- SimpleWebService.AddComplexNumber(cplx1, cplx2, onAddComplextNumberComplete);
- }
- function onAddComplextNumberComplete(result) {
- $('btnAddComplex').value = result.Real.toString() + ' + ' + result.Imag.toString() + 'i';
- }
浏览器中运行一下,初始化:
在ASP.NET Atlas中调用Web Service——介绍及简单应用相关推荐
- 在ASP.NET Atlas中调用Web Service——创建Mashup调用远端Web Service(基础知识以及简单示例)...
作者:Dflying Chen (http://dflying.cnblogs.com/) 注:Atlas中的Mashup极其复杂,其中涉及众多的对象与架构,为了写这篇文章,我花了不少时间学习研究.同 ...
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(2):处理异步调用中的异常...
本文来自<ASP.NET AJAX程序设计 第II卷:客户端Microsoft AJAX Library相关>的第三章<异步调用Web Service和页面中的类方法>,请同时 ...
- ASP.NET MVC4中调用WEB API的四个方法
当今的软件开发中,设计软件的服务并将其通过网络对外发布,让各种客户端去使用服务已经是十分普遍的做法.就.NET而言,目前提供了Remoting,WebService和WCF服务,这都能开发出功能十分强 ...
- Web service 介绍与简单应用
一.Web service 介绍 什么是 Web service 到目前为止对 Web service 没有统一的定义,这里以一种更容易理解的方式解释.Web service 是一个平台独立的,低耦合 ...
- 前台页面通过ajax环境和js调用web service的几种方法
目录: 1.配置ajax环境 2.js中调用web service (ajax环境) 方法一 3.方法二 4.方法三 5.ajax中的错误处理 //6.写的比较快 如果有疏忽之处 如能指出感激不尽. ...
- VB.NET,C#.NET调用Web Service,利用visual studio 的实现方法
在VB.NET调用Web Service提供的服务 技术qq交流群:JavaDream:251572072 下面是一篇文章比较详细,其实具体操作很简单,把Web Service服务地址,利用工具(VS ...
- Visual Studio 2013中引入Web Service的简单方法visual studio 引用 wsdl
http://blog.csdn.net/wangzhongbo_24/article/details/49954191 Web Service有三种表示方式 三种方式分别为WSDL.Endpoint ...
- .NET应用程序中异步调用Web Service的几种方法 come from: veryhappy(wx.net)
测试程序界面 图一,调用前界面 图二,调用后界面 详细代码实现 闲言少叙,直接进入主题吧.首先一个声明一个类(将来在客户端与服务器间传递): public class Class1 { ...
- .NET2.0中,Winform程序如何异步调用Web Service呢?[Demo下载]——与.net1.1环境下比较...
最近在MSDN上看到一个在.NET1.1中Winform程序异步调用WebService的例子 我准备模仿着迁移到.NET2.0环境中,遗憾的是,一切不是那么简单. 首先,.net1.1中调用的Web ...
- C#之VS2010ASP.NET页面调用Web Service和winform程序调用Web Service
一:用ASP.NET调用Web Service 打开VS2010,打开"文件-新建-网站",选择"ASP.NET网站" 选好存储位置,语言后点击确定,进入默认页 ...
最新文章
- P4161 [SCOI2009]游戏
- 【设计模式:单例模式】使用单例模式加载properties文件
- stm32之RCC寄存器学习
- ABB 机器人 压包指令PackRawBytes 解包指令UnpackRawBytes
- C排序算法:(二)冒泡排序
- NLP之路-Dataset大全
- Android 系统签名实现的三种方式
- 条形码的码制分类详解
- windows防火墙出站规则只允许访问指定域名ip,其他出站ip全部阻止的方法
- python 验证码识别
- 从0到1的电商架构应该怎么做?有哪些坑?
- Python爬虫天气预报(小白入门)
- vue RSA加密算法(jsencrypt)的使用
- Redux源码分析--Enhancer
- 修改伪造Flash版本号
- 入会领京豆Python脚本
- python有哪几种模块_python常用模块有哪些?
- Mysql--day03
- NetBox使用方法
- Markdown 数学公式以及符号整理
热门文章
- 内蒙古师范大学计算机与信息工程学院--《大数据存储与处理》期末考试试卷...
- Linux sed 批量替换多个文件中的字符串【转载】
- Sqlserver2005附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法...
- Cmdkey 凭证管理器工具
- Flex中实现Tree绑定数据后自动展开节点
- 应用场景|R包分类整理
- sparklyr 1.0发布,有哪些新功能?
- 洛谷 P1113 杂务
- (原)数据结构之树状数组详解
- Spring Boot 2.x(十四):整合Redis,看这一篇就够了