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

在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 这个链接:


 

点击页面上的 叫用 按钮:

 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值