在 Visual Studio 2010 中创建 ASP.Net 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 SecondNet {/// <summary>/// WebService1 的摘要說明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.ComponentModel.ToolboxItem(false)]// 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的注釋。// [System.Web.Script.Services.ScriptService]public class WebService1 : System.Web.Services.WebService{[WebMethod]public string HelloWorld(){return "Hello World";}} }直接Press F5就可以看到結果:
然后我們改寫這段代碼,添加我們自己的方法進去:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services;namespace SecondNet {/// <summary>/// WebService1 的摘要說明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.ComponentModel.ToolboxItem(false)]// 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的注釋。// [System.Web.Script.Services.ScriptService]public class WebService1 : System.Web.Services.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,這才是最合理的作法。
轉載于:https://www.cnblogs.com/longshiyVip/p/4966536.html
總結
以上是生活随笔為你收集整理的在 Visual Studio 2010 中创建 ASP.Net Web Service的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java中如何利用gson解析数据
- 下一篇: Ajax:一种网页开发技术(Asynch