close

先建立一個空的Web應用程式

image


然後在這個專案中  [加入] [新增項目]

image

這樣就建立好了一個Web Service

將這個Web Service 用瀏覽器檢視看看

image

image

頁面會顯示這一個Web Service,可供被呼叫的Method


每一個Method都可以被規範好傳進來的是甚麼,回傳的是甚麼.下面就是簡單的例子,傳入兩個字串參數,回傳一個物件(Model)

先建立一個ArticleModel

   1: public class Article
   2: {
   3:     public string GUID { get; set; }
   4:     public string Title { get; set; }
   5:     public string Contents { get; set; }
   6: }

然後撰寫一個Method

   1: [WebMethod]
   2: public Article GetArticle(string x, string y)
   3: {
   4:  
   5:     var art = new Article();
   6:     art.GUID = "GUID"+ x ;
   7:     art.Title = "TITLE" + y;
   8:     return  art;
   9: }

再回到剛剛的Method清單列表頁面點選剛剛做的GetArticle

image

輸入傳值的參數後,Web Service就會回傳相對應的結果.

網頁顯示畫面如下 ,

image


但是真正的回傳內容是XML 剛剛那個是顯示用的,實際上的資料是長這樣

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <Article xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
   3:   <GUID>GUID測試</GUID>
   4:   <Title>TITLET00001</Title>
   5: </Article>
arrow
arrow
    全站熱搜

    baechang 發表在 痞客邦 留言(0) 人氣()