在 Visual Studio 2012 中创建 ASP.Net Web Service

来源:互联网 发布:r230清零软件图解 编辑:程序博客网 时间:2024/06/05 16:19

实践检验,Visual Studio 2013页是类似

在 Visual Studio 2012 中创建 ASP.Net Web Service,步骤非常简单。如下:

第一步:创建一个“ASP.Net Empty Web Application”项目

创建一个“ASP.Net Empty Web Application”项目。你会看到一个进度条,显示 Visual Studio 2012 正在创建这个空的 ASP.Net Web Application。

经历短暂的等待之后,一个空的 ASP.Net Web Application 就建好了,它仅包含一个站点配制文件(Web.config),其余的什么也没有。

第二步:在项目中添加“Web Service”新项目

在 Visual Studio 2012 的 Solution Explorer 中,选中当前的这个 project,添加新项目(右键菜单:Add --> New Item),选择“Web Service”这种类型:

第三步:编码、运行

添加完Web Service这种 new item 之后,Visual Studio 已经替我们写了个示范的Web方法了:

直接按快捷键 F5 就可以看到结果:

点击 HelloWorld 这个链接:

点击页面上的 Invoke 按钮:

然后我们改写这段代码,添加我们自己的方法进去:

[csharp] view plaincopyprint?
  1. namespace WebApplication1  
  2. {  
  3.     using System.Web.Services;  
  4.   
  5.     /// <summary>  
  6.     /// Summary description for WebService1  
  7.     /// </summary>  
  8.     [WebService(Namespace = "http://tempuri.org/")]  
  9.     public class WebService1 : System.Web.Services.WebService  
  10.     {  
  11.         [WebMethod]  
  12.         public string HelloWorld()  
  13.         {  
  14.             return "Hello World";  
  15.         }  
  16.   
  17.         [WebMethod]  
  18.         public int Add(int x, int y)  
  19.         {  
  20.             return x + y;  
  21.         }    
  22.     }  
  23. }  


运行:

点击 Add 链接,调用我们刚刚自己添加的 Add 函数:

点击页面上的 Invoke 按钮,得到以 XML 格式返回的执行结果:

+

整个过程非常简单、直观。


参考:http://blog.csdn.net/yapingxin/article/details/7993989


0 0
原创粉丝点击