在ASP.NET Atlas中調(diào)用Web Service_.Net教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:ASP.NET中MD5和SHA1加密的幾種方法
      MD5的全稱是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由Mit Laboratory for Computer Science和Rsa data security inc的Ronald l. rivest開發(fā)出來,經(jīng)md2、md3和md4發(fā)展而來。它

      Atlas Framework中包含了對AJAX調(diào)用的封裝,讓您可以很方便的在客戶端通過JavaScript調(diào)用服務(wù)器端方法。在本篇文章中,我將解釋一下如何使用Atlas調(diào)用服務(wù)器端Web Service。

      使用Atlas,我們只需要如下步驟即可調(diào)用服務(wù)器端Web Service:

      在Web Service的方法上加上[WebMethod]屬性。

      在ASPX頁面上的ScriptManager中添加對這個Web Service的引用。

      只需以上兩步,Atlas會在運(yùn)行時為您生成相應(yīng)的mash up,讓您可在客戶端JavaScript中通過WebServiceClassName.ServiceMethodName()調(diào)用該方法。

      讓我們先來看一個最簡單的例子,調(diào)用服務(wù)器端Web Service得到兩個數(shù)的和:

      首先建立一個Web Service:SimpleWebService.asmx,并在其中添加一個Service Method,不要忘記標(biāo)記為[WebMethod]哦: [WebMethod]
      public int AddInt(int int1, int int2)
      {
      return int1 int2;
      }
      然后再ASPX頁面上的ScriptManager中添加對該Web Service的引用:

      上面的例子僅僅傳遞簡單類型,然而在現(xiàn)實(shí)世界中,我們經(jīng)常會需要傳遞一些復(fù)雜的類型,讓我們看一個傳遞復(fù)雜類型的例子:

      本例子同樣是一個加法,不過這回操作的類型是復(fù)數(shù)。讓我們先來看看C#中我們的復(fù)數(shù)的定義(作為示例,這里盡可能的簡化)。注意我們應(yīng)該提供自定義的復(fù)雜類型一個無參的構(gòu)造函數(shù),以便于Atlas自動在C#類型和JavaScript類型中轉(zhuǎn)換: 

      public class ComplexNumber
        {
         private int real;
        
         public int Real
         {
         get { return real; }
         set { real = value; }
         }
         private int imag;
        
         public int Imag
         {
         get { return imag; }
         set { imag = value; }
         }
         public ComplexNumber(int real, int imag)
         {
         this.real = real;
         this.imag = imag;
         }
         public ComplexNumber()
         {
         }
        }

      然后是實(shí)現(xiàn)復(fù)數(shù)加法的Web Method,寫在同一個Web Service中:  

      [WebMethod]
        public ComplexNumber AddComplexNumber(ComplexNumber num1, ComplexNumber num2)
        {
         return new ComplexNumber(num1.Real num2.Real, num1.Imag num2.Imag);
        }

      相應(yīng)的ASPX頁面中也要添加一些HTML,讓用戶輸入兩個復(fù)數(shù):

      Pass complex type to web service - add the two complex numbers:
        

        然后是相應(yīng)的JavaScript,當(dāng)用戶點(diǎn)擊上面的按鈕時,執(zhí)行這段JavaScript以調(diào)用Web Method。
        function btnAddComplex_onclick() {
         var cplx1 = {Real: $('cplx1r').value, Imag: $('cplx1i').value};
         var cplx2 = {Real: $('cplx2r').value, Imag: $('cplx2i').value};
         SimpleWebService.AddComplexNumber(cplx1, cplx2, onAddComplextNumberComplete);
        }
        function onAddComplextNumberComplete(result) {
         $('btnAddComplex').value = result.Real.toString() ' ' result.Imag.toString() 'i';  }

      添加點(diǎn)HTML Code,讓用戶輸入兩個整數(shù): Pass simple type to web service - add the two integers: 

      再書寫一點(diǎn)JavaScript,當(dāng)用戶點(diǎn)擊上面的按鈕時,調(diào)用Web Method。這里要注意的是JavaScript中調(diào)用Web Method的格式:前面兩個參數(shù)int1,int2分別對應(yīng)著Web Service聲明中的兩個參數(shù),后面一個參數(shù)onAddIntComplete表示方法成功返回時的Callback方法,也就是所謂AJAX中的A。同時需要注意的是$()方法,等同于document.getElementById()。

      function btnAddInt_onclick() {
         var int1 = $('int1').value;
         var int2 = $('int2').value;
         SimpleWebService.AddInt(int1, int2, onAddIntComplete);
        }
        function onAddIntComplete(result) {
         $('btnAddInt').value = result;
        }

      分享:ASP.NET 2.0當(dāng)中的Call Back機(jī)制
      用過ASP.NET 2.0當(dāng)中的新功能Call Back的朋友們可能會發(fā)現(xiàn):ASP.NET AJAX當(dāng)中的非同步Web Services調(diào)用很麻煩,還要多建立一個Web Sevices(.asmx)頁面,如果您嫌額外建立一個.asmx的Web Service

      來源:模板無憂//所屬分類:.Net教程/更新時間:2008-08-22
      相關(guān).Net教程