在 Visual Studio 2010 中创建 ASP.Net Web Service
第一步:創(chuàng)建一個(gè)“ASP.Net Empty Web Application”項(xiàng)目
第二步:在項(xiàng)目中添加“Web Service”新項(xiàng)目
第一步之后,Visual Studio 2010會(huì)創(chuàng)建一個(gè)僅含一個(gè)站點(diǎn)配制文件(Web.config)的空站點(diǎn),其余的什么也沒(méi)有。
我們?cè)赩isual Studio 2010的Solution Explorer中,選中當(dāng)前的這個(gè)project,添加新項(xiàng)目(右鍵菜單:Add --> New Item),選擇“Web Service”這種類型:
看到這里讀者應(yīng)該就恍然大悟了吧。
好,我們繼續(xù):
第三步:編碼、運(yùn)行
添加完Web Service這種new item之后,Visual Studio已經(jīng)替我們寫了個(gè)示范的Web方法了:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services;namespace SecondNet {/// <summary>/// WebService1 的摘要說(shuō)明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.ComponentModel.ToolboxItem(false)]// 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消對(duì)下行的注釋。// [System.Web.Script.Services.ScriptService]public class WebService1 : System.Web.Services.WebService{[WebMethod]public string HelloWorld(){return "Hello World";}} }直接Press F5就可以看到結(jié)果:
然后我們改寫這段代碼,添加我們自己的方法進(jìn)去:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services;namespace SecondNet {/// <summary>/// WebService1 的摘要說(shuō)明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.ComponentModel.ToolboxItem(false)]// 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消對(duì)下行的注釋。// [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;} } }運(yùn)行:
?
?
?
怎么樣,是不是很簡(jiǎn)單?
總結(jié)
現(xiàn)在我們?cè)倩剡^(guò)頭來(lái)看看,從VS2010之前版本的舊的創(chuàng)建Web Service的方式,到現(xiàn)在新的變化,Visual Studio改動(dòng)了什么?
手頭的機(jī)器沒(méi)有裝舊版的Visual Studio,我就現(xiàn)從網(wǎng)上抓一張教程里的截圖吧,讓我們看看舊版的Visual Studio里面大家創(chuàng)建Web Service時(shí)創(chuàng)建新項(xiàng)目的截圖:
很多人說(shuō)在Visual Studio 2010里面無(wú)法創(chuàng)建Web Service,他們大概是在尋找上面截圖中的這種“ASP.Net Web Service”項(xiàng)目吧。
現(xiàn)在再回過(guò)頭來(lái)看看,其實(shí)微軟在Visual Studio 2010里面作了一個(gè)相當(dāng)合理(make sense)的改變。
Web Service并不能單獨(dú)存在,它必須Host在一個(gè)Web Site/Web Application上面。所以,在一個(gè)Web Site/Web Application里面,通過(guò)Add new item添加一個(gè)Web Service,這才是最合理的作法。
轉(zhuǎn)載于:https://www.cnblogs.com/longshiyVip/p/4966536.html
總結(jié)
以上是生活随笔為你收集整理的在 Visual Studio 2010 中创建 ASP.Net Web Service的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java中如何利用gson解析数据
- 下一篇: asp.net ajax控件工具集 Au