现在的位置: 首页 > 综合 > 正文

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

2012年08月08日 ⁄ 综合 ⁄ 共 894字 ⁄ 字号 评论关闭

在 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 按钮:

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

namespace WebApplication1
{
    using System.Web.Services;

    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    public class WebService1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public int Add(int x, int y)
        {
            return x + y;
        }  
    }
}

运行:

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

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

+

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

【上篇】
【下篇】

抱歉!评论已关闭.