(转)在 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 应用 ...
随机推荐
- WEB服务器、应用程序服务器区别
WEB服务器.应用程序服务器.HTTP服务器有何区别?IIS.Apache.Tomcat.Weblogic.WebSphere都各属于哪种服务器,这些问题困惑了很久,今天终于梳理清楚了: Web服务器 ...
- RMAN 备份
backup database; --备份整库 backup database format '\xxxxxx\xxx_%U'; --备份整库到指定路劲 backup tablespace users ...
- Effective C++_笔记_条款05_了解C++默认编写并调用哪些函数
(整理自Effctive C++,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 直接上代码: 1: class Empty{}; 如果你写了这样 ...
- Android SDK 环境变量配置(Windows)
Android 开发需要进行adb的配置, 这里使用的是 adt-bundle-windows-x86_64-20140321, 里面捆绑的有 eclipse ,不需要再进行 其他的配置,直接下载解压 ...
- C#递归复制文件夹
/// <param name="sources">原路徑</param> /// <param name="dest">目 ...
- 基于visual Studio2013解决面试题之0309左移递减序列搜索
题目
- Documentation | AnsibleWorks
Documentation | AnsibleWorks Welcome to the Ansible documentation! Ansible is an IT automation too ...
- 框架基础JNI
转载请标明出处: http://blog.csdn.net/yujun411522/article/details/46342793 本文出自:[yujun411522的博客] 2.1 概述 JNI ...
- lua 函数回调技巧
技巧1: local a = {}; function b() print("Hello World") end a["sell"] = {callFunc = ...
- iOS - NSLog的使用方法
NSLog的定义 NSLog定义在NSObjCRuntime.h中,如下所示: void NSLog(NSString *format, …); 基本上,NSLog很像printf,同样会在conso ...