ASP.NET調(diào)用Web Services方法
在Atlas中,它的“Web Services” 被放在了一個(gè)特殊的運(yùn)行環(huán)境中執(zhí)行(在某些情況下會委托給ASP.NET原有組件執(zhí)行,這點(diǎn)在之前的文章中有過分析),因此,即使我們不是通過AJAX方式訪問,只要了解Atlas那一套特殊的運(yùn)行環(huán)境的行為,依舊能夠給我們帶來一些別的使用方式。下面的示例就將使用Atlas服務(wù)器端對于Web Services調(diào)用的支持,來講解如何使用HTTP GET來調(diào)用Web Services方法(除非特別說明,以下所有的解釋均針對Atlas的擴(kuò)展,而不是ASP.NET的原有Web Services支持)。
首先,我們寫一個(gè)Web Serivces方法:
- [WebMethod]
 - [WebOperation(true, ResponseFormatMode.Xml)]
 - public XmlDocument Vote(string name, int id)
 - {
 - XmlDocument responseDoc = new XmlDocument();
 - responseDoc.LoadXml(
 - "<?xml-stylesheet type=\"text/xsl\" href=\"Vote.xsl\"?>" +
 - "<response><user></user><id></id></response>");
 - responseDoc.SelectSingleNode("http://user").InnerText = name;
 - responseDoc.SelectSingleNode("http://id").InnerText = id.ToString();
 - return responseDoc;
 - }
 
在Atlas中,HTTP POST為Web Services的默認(rèn)支持方法,也是必然的支持方法。而如果需要使該Web Service方法支持HTTP GET的話,就必須如上面代碼一樣,使用Microsoft.Web.Services.WebOperationAttribute進(jìn)行標(biāo)注。 WebOperationAttribute的***個(gè)參數(shù)就是getVerbEnabled,true則表示支持HTTP GET方法。第二個(gè)參數(shù)Microsoft.Web.Services.ResponseFormatMode.Xml則表示結(jié)果對象的輸出方式為 XML,而不是默認(rèn)的JSON。
在這里,我們使用XML的原因是因?yàn)镴SON在這里沒有任何意義。返回JSON后是為了在獲得這些內(nèi)容之后通過Javascript函數(shù)eval執(zhí)行,從而獲得JSON表示的對象。而在這里,我們的目的是將結(jié)果顯示給用戶看,所以使用XML形式返回,再加上XSL的支持,就能以HTML的形式顯示給用戶了。
然后就是簡單的XSL:
- <?xml version="1.0" encoding="utf-8"?>
 - <xsl:stylesheet version="1.0"
 - xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 - <xsl:template match="/response">
 - <html>
 - <head>
 - <title>Thanks for your participation.</title>
 - </head>
 - <body style="font-family:Verdana; font-size:13px;">
 - <h4>Thanks for your participation.</h4>
 - <div>
 - <xsl:text>Dear </xsl:text>
 - <xsl:value-of select="user"/>
 - <xsl:text>, you've voted for item </xsl:text>
 - <xsl:value-of select="id"/>
 - <xsl:text>.</xsl:text>
 - </div>
 - </body>
 - </html>
 - </xsl:template>
 - </xsl:stylesheet>
 
接下來就是我們的HTML文件。我們的目的非常簡單,就是得到用戶輸入的信息,拼接成URL之后在新窗口中打開。因此我們在這里根本無需使用Atlas。代碼如下:
- <div>Name:<input type="text" id="txtName" /></div>
 - <div>Item:
 - <select id="comboItem">
 - <option value="1">Item 1</option>
 - <option value="2">Item 2</option>
 - <option value="3">Item 3</option>
 - <option value="4">Item 4</option>
 - <option value="5">Item 5</option>
 - </select>
 - </div>
 - <input type="button" value="Vote" onclick="vote()" />
 
點(diǎn)擊“Vote”按鈕后,就會調(diào)用Javascript函數(shù)Vote()。代碼如下:
- <script language="javascript">
 - function vote()
 - {
 - var url = "HttpGetWebService.asmx?mn=Vote";
 - url += ("&name=" + encodeURI(document.getElementById("txtName").value));
 - url += ("&id=" + document.getElementById("comboItem").value);
 - window.open(url);
 - }
 - </script>
 
我們需要拼接的URL很簡單:首先使用在 QueryString里將mn設(shè)為我們即將調(diào)用Web Services方法名,然后就是在QueryString里調(diào)用Web Services方法所需的參數(shù)了。請注意,既然是使用URL拼接,那么就必須使用encodeURI進(jìn)行編碼后才能使用,否則可能會出現(xiàn)異常情況。以上介紹ASP.NET調(diào)用Web Services方法。
【編輯推薦】















 
 
 
 
 
 
 