在Visual Studio 2010新建Web应用程序或者Web网站窗口中,如果你选择.NET 4.0,会发现缺少ASP .Net Web Service模板。
可以通过以下两种方式建立ASP .NET Web Services项目或网站:
1. 可以先选择.NET 2.0/3.0/3.5,就可以选择ASP .NET Web Services模板建立了,之后如果需要.NET 4.0的新特性,可以在项目属性窗口或网站属性页窗口的Build选项卡中选择.NET4.0.
2. 如果直接选择.NET 4.0
第一步:创建一个“ASP.Net Empty Web Application”项目
完成后一个空的 ASP.Net WebApplication 就建好了,它仅包含一个站点配制文件(Web.config),其余的什么也没有。
第二步:在项目中添加“WebService”新项目
在Visual Studio 2010 的Solution Explorer 中,选中当前的这个project,添加新项目(右键菜单:Add--> New Item),选择“WebService”这种类型:
第三步:编码、运行
添加完WebService这种new item 之后,VisualStudio 已经替我们写了个示范的Web方法了:
<span style="font-family:Microsoft YaHei;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebServiceTest
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}</span>
直接按快捷键 F5 就可以看到结果:
点击 HelloWorld 这个链接:
点击页面上的 叫用 按钮:
整个过程非常简单、直观。