ITeye博客

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

sangei · 2014-02-22 21:52 · SOA · 编程语言 · Markdown

在 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. /// <summary>
  5. /// Summary description for WebService1
  6. /// </summary>
  7. [WebService(Namespace = "http://tempuri.org/")]
  8. public class WebService1 : System.Web.Services.WebService
  9. {
  10. [WebMethod]
  11. public string HelloWorld()
  12. {
  13. return "Hello World";
  14. }
  15. [WebMethod]
  16. public int Add(int x, int y)
  17. {
  18. return x + y;
  19. }
  20. }
  21. }

运行:

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

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

+

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。