ASP.NET 中创建Web Services及简单调用

在ASP.NET中创建Web服务很简单,因为大多数复杂性都由.NET平台处理了,事实上,创建一个Web服务几乎与在C#一个方法一样简单。以下是一个Web服务创建实例:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace WebService1
{
    
/// <summary>
    
/// Service2 的摘要说明。
    
/// </summary>

    public class Service2 : System.Web.Services.WebService
    
{
            [WebMethod]
        
public string Add(int x,int y)
    
        
{
            
int z=x+y;
            
return z.ToString(); 
        
        }

    }

}

创建完成后需要在工程中添加一个Html页面进行调用,以下是完整代码:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    
<body>

    
<p>使用POST方法调用WEB服务</p>
    
<form name="form3" method=post action=Service2.asmx/Add id="Form3">
        
<P>
            请输入第一个数字:
<input type="text" name="x" id="Text1">
            
<br>
            请输入第二个数字:
<input type="text" name="y" id="Text2">
        
</P>
        
<P>
            
<input type="submit" id="submit1" name="submit1" value="提交查询内容">
        
</P>
    
</form>
    
</body>
</HTML>
最后设置Html页面为启动项,生成解决方案后启动项目,输入两个数字,得到如下结果:
 <?xml version="1.0" encoding="utf-8" ?> 
  
<string xmlns="http://tempuri.org/">11</string> 
至此,我们创建的Web服务被成功调用。
posted on 2006-02-08 17:33  foson  阅读(476)  评论(1编辑  收藏  举报