在 Visual Studio 2012 中創建 ASP.Net Web Service

實踐檢驗,Visual Studio 2013頁是類似

在 Visual Studio 2012 中創建 ASP.Net Web Service,步驟非常簡單。如下:

第一步:創建一個“ASP.Net Empty Web Application”項目

創建一個“ASP.Net Empty Web Application”項目。你會看到一個進度條,顯示 Visual Studio 2012 正在創建這個空的 ASP.Net Web Application。

經歷短暫的等待之後,一個空的 ASP.Net Web Application 就建好了,它僅包含一個站點配製文件(Web.config),其餘的什麼也沒有。

第二步:在項目中添加“Web Service”新項目

在 Visual Studio 2012 的 Solution Explorer 中,選中當前的這個 project,添加新項目(右鍵菜單:Add --> New Item),選擇“Web Service”這種類型:

第三步:編碼、運行

添加完Web Service這種 new item 之後,Visual Studio 已經替我們寫了個示範的Web方法了:

直接按快捷鍵 F5 就可以看到結果:

點擊 HelloWorld 這個鏈接:

點擊頁面上的 Invoke 按鈕:

然後我們改寫這段代碼,添加我們自己的方法進去:

  1. namespace WebApplication1  
  2. {  
  3.     using System.Web.Services;  
  4.   
  5.     /// <summary>  
  6.     /// Summary description for WebService1  
  7.     /// </summary>  
  8.     [WebService(Namespace = "http://tempuri.org/")]  
  9.     public class WebService1 : System.Web.Services.WebService  
  10.     {  
  11.         [WebMethod]  
  12.         public string HelloWorld()  
  13.         {  
  14.             return "Hello World";  
  15.         }  
  16.   
  17.         [WebMethod]  
  18.         public int Add(int x, int y)  
  19.         {  
  20.             return x + y;  
  21.         }    
  22.     }  
  23. }  


運行:

點擊 Add 鏈接,調用我們剛剛自己添加的 Add 函數:

點擊頁面上的 Invoke 按鈕,得到以 XML 格式返回的執行結果:

+

整個過程非常簡單、直觀。


參考:http://blog.csdn.net/yapingxin/article/details/7993989


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章