在 Visual Studio 2010 中创建 ASP.Net Web Service
来源:互联网 发布:数据录入员有升职吗 编辑:程序博客网 时间:2024/05/29 15:50
很多人在论坛里说,在Visual Studio 2010中不能创建“ASP.Net Web Service”这种project了,下面跟帖者云云,有的说这是因为微软已经将Web Service整合进WCF,也有的提出一种先将.Net Framework Target设置为3.5的一种很“Tricky”的作法,其实这些说法是不准确的。微软确实用WCF整合了Web Service,但并不等于说微软不准备让大家在Visual Studio里面创建传统的Web Service了。其实正确的做法很简单,大家一看就恍然大悟了。
第一步:创建一个“ASP.Net Empty Web Application”项目
第二步:在项目中添加“Web Service”新项目
第一步之后,Visual Studio 2010会创建一个仅含一个站点配制文件(Web.config)的空站点,其余的什么也没有。
我们在Visual Studio 2010的Solution Explorer中,选中当前的这个project,添加新项目(右键菜单:Add --> New Item),选择“Web Service”这种类型:
看到这里读者应该就恍然大悟了吧。
好,我们继续:
第三步:编码、运行
添加完Web Service这种new item之后,Visual Studio已经替我们写了个示范的Web方法了:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- namespace sitedemo.Services
- {
- /// <summary>
- /// Summary description for CalculateService
- /// </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 CalculateService : System.Web.Services.WebService
- {
- [WebMethod]
- public string HelloWorld()
- {
- return "Hello World";
- }
- }
- }
直接Press F5就可以看到结果:
然后我们改写这段代码,添加我们自己的方法进去:
- using System.Web.Services;
- namespace sitedemo.Services
- {
- /// <summary>
- /// Summary description for CalculateService
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- public class CalculateService : WebService
- {
- [WebMethod]
- public string HelloWorld()
- {
- return "Hello World";
- }
- [WebMethod]
- public int Add(int x, int y)
- {
- return x + y;
- }
- }
- }
运行:
怎么样,是不是很简单?
总结
现在我们再回过头来看看,从VS2010之前版本的旧的创建Web Service的方式,到现在新的变化,Visual Studio改动了什么?
手头的机器没有装旧版的Visual Studio,我就现从网上抓一张教程里的截图吧,让我们看看旧版的Visual Studio里面大家创建Web Service时创建新项目的截图:
很多人说在Visual Studio 2010里面无法创建Web Service,他们大概是在寻找上面截图中的这种“ASP.Net Web Service”项目吧。
现在再回过头来看看,其实微软在Visual Studio 2010里面作了一个相当合理(make sense)的改变。
Web Service并不能单独存在,它必须Host在一个Web Site/Web Application上面。所以,在一个Web Site/Web Application里面,通过Add new item添加一个Web Service,这才是最合理的作法。
- 在 Visual Studio 2010 中创建 ASP.Net Web Service
- 在 Visual Studio 2010 中创建 ASP.Net Web Service
- 在 Visual Studio 2010 中创建 ASP.Net Web Service
- 在 Visual Studio 2010 中创建 ASP.Net Web Service
- 在 Visual Studio 2012 中创建 ASP.Net Web Service
- 在 Visual Studio 2012 中创建 ASP.Net Web Service
- 在 Visual Studio 2012 中创建 ASP.Net Web Service
- (转)在 Visual Studio 2010 中创建 ASP.Net Web Service
- 演练:在 Visual Web Developer 中创建和使用 ASP.NET Web 服务------Web Service
- 在 Visual Studio 2013 中创建 ASP.NET Web 项目(1):概述 - 创建 Web 应用程序项目
- Walkthrough: Creating and Using an ASP.NET Web Service in Visual Web Developer Visual Studio 2010 O
- 为何我不能在我的VISUAL STUDIO .NET中创建ASP.NET项目(程序)
- 用 Visual Studio 2010 在 SharePoint 2010 创建ASMX Web Service
- 在Visual Studio 2005 Web Application Project中使用ASP.NET AJAX
- Visual studio 2012 创建web service
- ASP.NET 4 和 Visual Studio 2010 Web 开发概述
- 如何使用 Visual Studio .NET 和 Office xp 创建和部署 XML Web service
- ASP.NET 5系列教程 (五):在Visual Studio 2015中使用Grunt、Bower开发Web程序
- 使用eclipse 搭建python开发环境
- RDO的openstack中安装savanna 0.3 --by Ethan_lynn
- ESRI.ArcGIS.RuntimeManager.Bind返回false解决方法
- 解决'RedisException' with message 'read error on connection'
- [升级] 数据库升级到11g的应急方案
- 在 Visual Studio 2010 中创建 ASP.Net Web Service
- 好记性不如烂笔头- linux 下rpm软件的安装和卸载 rpm --force -ivh ...
- 卡方检验基础
- SPOJ 8628 Selling Land
- HTTP Status 500 ?org.apache.jasper.JasperException: /date.jsp(0,0) Page directive: can't have multip
- 扩展DynamicMapServiceLayer加载服务器TIF数据
- Amazon AppStream服务介绍
- 在Win2003、Win7、XP中配置IIS搭建ASP.NET Web网站或应用程序
- EC2使用iostat, netstat,需安装