天天看點

C#進階系列——WebApi 接口測試工具:WebApiTestClient一、WebApiTestClient介紹二、WebApiTestClient展示三、WebApiTestClient使用四、總結

前言:這兩天在整WebApi的服務,由于調用方是Android用戶端,Android開發人員也不懂C#文法,API裡面的接口也不能直接給他們看,沒辦法,隻有整個詳細一點的文檔呗。由于接口個數有點多,每個接口都要詳細說明接口作用、參數類型、傳回值類型等等,寫着寫着把部落客惹毛了,難道這種文檔非要自己寫不成?難道網上沒有這種文檔的展示工具嗎?帶着這兩個問題,在網絡世界裡尋找,網絡世界很奇妙,隻要你用心,總能找到或多或少的幫助!這不就被部落客找到了這個好用的元件:WebApiTestClient。它對于接口的作用、參數作用以及類型、傳回值說明等等都能很好展現,并且提供測試調用的功能,輸入測試參數發送請求,就能得到相應的傳回結果。哇塞,這一系列可視化的效果不正是部落客需要的嗎,還寫什麼文檔,趕緊用起來!

WebApi系列文章

<a href="http://www.cnblogs.com/landeanfen/p/5210356.html">C#進階系列——WebApi接口測試工具:WebApiTestClient</a>

<a href="http://www.cnblogs.com/landeanfen/p/5177176.html">C#進階系列——WebApi 跨域問題解決方案:CORS</a>

<a href="http://www.cnblogs.com/landeanfen/p/5287064.html">C#進階系列——WebApi身份認證解決方案:Basic基礎認證</a>

<a href="http://www.cnblogs.com/landeanfen/p/5337072.html">C#進階系列——WebApi接口傳參不再困惑:傳參詳解</a>

<a href="http://www.cnblogs.com/landeanfen/p/5501487.html">C#進階系列——WebApi接口傳回值不困惑:傳回值類型詳解</a>

C#進階系列——WebApi異常處了解決方案

C#進階系列——WebApi區域Area使用小結

1、WebApiTestClient元件作用主要有以下幾個:

(1)、将WebApi的接口放到了浏覽器裡面,以可視化的方式展現出來,比如我們通過http://localhost:8080/Help這個位址就能在浏覽器裡面看到這個服務裡面所有的API接口以及接口的詳細說明,省去了我們手寫接口文檔的麻煩。

(2)、能夠詳細檢視API的類說明、方法說明、參數說明、傳回值說明。隻需要我們在定義方法時候加上 /// &lt;summary&gt; 這種詳細注釋即可,元件自動讀取注釋裡面的内容展現在界面上面。

(3)、可以修改http請求頭檔案Head和請求體Body裡面的參數,指定發送http請求的特性,比如指定我們最常見的contentType訓示參數的類型。

(4)、元件擁有測試接口的功能,用過Soup UI的朋友應該知道,通過Soup UI能夠友善測試WebService參數以及傳回值。我們的WebApiTestClient也可以實作類似的功能,直接通過頁面上的測試按鈕,就能測試接口。

第一印象:接口清單。 

C#進階系列——WebApi 接口測試工具:WebApiTestClient一、WebApiTestClient介紹二、WebApiTestClient展示三、WebApiTestClient使用四、總結

點選某一個接口檢視接口詳細。例如本文檢視Get請求的無參方法,右下角有按鈕可以測試接口。

C#進階系列——WebApi 接口測試工具:WebApiTestClient一、WebApiTestClient介紹二、WebApiTestClient展示三、WebApiTestClient使用四、總結

點選“Test API”按鈕

C#進階系列——WebApi 接口測試工具:WebApiTestClient一、WebApiTestClient介紹二、WebApiTestClient展示三、WebApiTestClient使用四、總結

點選Send發送請求

C#進階系列——WebApi 接口測試工具:WebApiTestClient一、WebApiTestClient介紹二、WebApiTestClient展示三、WebApiTestClient使用四、總結

第二個有參數的接口

C#進階系列——WebApi 接口測試工具:WebApiTestClient一、WebApiTestClient介紹二、WebApiTestClient展示三、WebApiTestClient使用四、總結

手動輸入參數,得到傳回結果

C#進階系列——WebApi 接口測試工具:WebApiTestClient一、WebApiTestClient介紹二、WebApiTestClient展示三、WebApiTestClient使用四、總結

如果參數的類型是對象,可以直接解析class定義上面的 /// &lt;summary&gt; 标注,顯示如下

C#進階系列——WebApi 接口測試工具:WebApiTestClient一、WebApiTestClient介紹二、WebApiTestClient展示三、WebApiTestClient使用四、總結

由于是post請求,如果需要執行參數長度和類型,可以通過content-length和content-type來指定。并且具體的參數可以指定不同格式顯示,比如下圖的application/json和application/xml

C#進階系列——WebApi 接口測試工具:WebApiTestClient一、WebApiTestClient介紹二、WebApiTestClient展示三、WebApiTestClient使用四、總結
C#進階系列——WebApi 接口測試工具:WebApiTestClient一、WebApiTestClient介紹二、WebApiTestClient展示三、WebApiTestClient使用四、總結

得到傳回值

C#進階系列——WebApi 接口測試工具:WebApiTestClient一、WebApiTestClient介紹二、WebApiTestClient展示三、WebApiTestClient使用四、總結

首先,我們需要定義一個API項目

C#進階系列——WebApi 接口測試工具:WebApiTestClient一、WebApiTestClient介紹二、WebApiTestClient展示三、WebApiTestClient使用四、總結

然後通過Nuget引入元件,如下圖。記住選下圖中的第三個。

C#進階系列——WebApi 接口測試工具:WebApiTestClient一、WebApiTestClient介紹二、WebApiTestClient展示三、WebApiTestClient使用四、總結

引入成功後,将向項目裡面添加一些主要檔案:

Scripts\WebApiTestClient.js

Areas\HelpPage\TestClient.css

Areas\HelpPage\Views\Help\DisplayTemplates\TestClientDialogs.cshtml

Areas\HelpPage\Views\Help\DisplayTemplates\TestClientReferences.cshtml

通過上述步驟,就能将元件WebAPITestClient引入進來。下面我們隻需要做一件事:打開檔案 (根據 Areas\HelpPage\Views\Help) Api.cshtml 并添加以下内容:

@Html.DisplayForModel("TestClientDialogs")

@Html.DisplayForModel("TestClientReferences")

添加後Api.cshtml檔案的代碼如下

其實,通過上面的步驟,我們的項目已經可以跑起來了,也可以調用接口測試。但是,還不能讀取 /// &lt;summary&gt; 注釋裡面的東西。需要做如下配置才行。

C#進階系列——WebApi 接口測試工具:WebApiTestClient一、WebApiTestClient介紹二、WebApiTestClient展示三、WebApiTestClient使用四、總結
C#進階系列——WebApi 接口測試工具:WebApiTestClient一、WebApiTestClient介紹二、WebApiTestClient展示三、WebApiTestClient使用四、總結

這句代碼如下:

至此,元件就搭完了,剩下的就是運作了。我們在url裡面敲位址http://localhost:8080/Help/Index或者http://localhost:8080/Help就能得到上述效果。還不趕緊試試~~