(转)在 Visual Studio 2010 中创建 ASP.Net Web Service
很多人在论坛里说,在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
转自:http://blog.csdn.net/xinyaping/article/details/7331375 很多人在论坛里说,在Visual Studio 2010中不能创建“ASP.Net ...
- 在 Visual Studio 2010 中创建 ASP.Net Web Service
第一步:创建一个“ASP.Net Empty Web Application”项目 第二步:在项目中添加“Web Service”新项目 第一步之后,Visual Studio 2010会创建一个仅含 ...
- 【转载】在 Visual Studio 2012 中创建 ASP.Net Web Service
在 Visual Studio 2012 中创建 ASP.Net Web Service,步骤非常简单.如下: 第一步:创建一个“ASP.Net Empty Web Application”项目 创建 ...
- 在 Visual Studio 2013 中创建 ASP.NET Web 项目(0):专题导航 [持续更新中]
写在前面的话 随着 Visual Studio 2013 的正式推出,ASP.NET 和 Visual Studio Web 开发工具 也发布了各自的最新版本. 新版本在构建 One ASP.NET ...
- 在 Visual Studio 2013 中创建 ASP.NET Web 项目(1):概述 - 创建 Web 应用程序项目
注:本文是“在 Visual Studio 2013 中创建 ASP.NET Web 项目”专题的一部分,详情参见 专题导航 . 预备知识 本专题适用于 Visual Studio 2013 及以上版 ...
- 简单记录在Visual Studio 2013中创建ASP.NET Web API 2
在很多跨平台的应用中就需要Web API ,比如android与数据库的交互. Create a Web API Project 选择新建项目下的模板下的Visual C#节点下的Web节点,在模板列 ...
- 在 Visual Studio 2010 中创建 SharePoint 2010 事件接收器
Microsoft Visual Studio 2010 提供了一个可用于生成事件接收器的项目类型,事件接收器会在 Microsoft SharePoint 2010 网站上选择事件之前或之后执行操作 ...
- 在Visual Studio 2010 中创建类库(dll)
创建类库 选择"文件"->新建->项目->Visual C# ->类库,输入名称,选择位置,单击确定 浏览解决方案资源管理器,可以看到两个C#类,第一个是A ...
- Visual Studio 2010 中的 Web 开发
概述 Microsoft Visual Studio 2010 为 ASP.NET Web 应用程序的开发提供非常多新的功能.这些新功能旨在帮助开发者高速方便地创建和部署质量高且功能全的 Web 应用 ...
随机推荐
- [LeetCode][Java] 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- 怎样查看apk须要支持的Android版本号
假设有一个apk,须要知道他最低安装支持的Android版本号是什么,应该怎样查看呢? 直接将apk后缀名改为rar或者zip,拉出AndroidManifest.xml?不行,AndroidMani ...
- Servlet的学习之Session(4)
在本篇中,我们来使用Session完成一个用户登录的案例,前提声明:这个案例主要用于学习Session技术,是属于比较简单的类型,以后会采用MVC模式来开发登录,那就会比较复杂. 现在大多数网站都提供 ...
- Selenium WebDriver ie,chrome 驱动
在驱动ie,chrome 的时候需要下载驱动 从网上下载IEDriverServer,Chromedriver 然后需要配置下就可以驱动ie,chrome 浏览器了 selenium 驱动ie 和 c ...
- Android SDK 环境变量配置(Windows)
Android 开发需要进行adb的配置, 这里使用的是 adt-bundle-windows-x86_64-20140321, 里面捆绑的有 eclipse ,不需要再进行 其他的配置,直接下载解压 ...
- 好多NFS的文章
http://www.cnblogs.com/lidabo/category/587288.html http://www.cnblogs.com/lidabo/p/4380555.html
- UML基本架构建模--类概述
Classes 类 Classes are the most important building block of any object-oriented system. A class is ...
- Windows XP 如何查看计算机开关机记录
在Windows XP中,我们可以通过“事件查看器”的事件日志服务查看计算机的开.关机时间.因为事件日志服务会随计算机一起启动和关闭,并在事件日志中留下记录. 在这里有必要介绍两个ID号:6006和6 ...
- 【ASP.NET Web API教程】4.3 ASP.NET Web API中的异常处理
原文:[ASP.NET Web API教程]4.3 ASP.NET Web API中的异常处理 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内 ...
- 与众不同 windows phone (26) - Contacts and Calendar(联系人和日历)
原文:与众不同 windows phone (26) - Contacts and Calendar(联系人和日历) [索引页][源码下载] 与众不同 windows phone (26) - Con ...