SwaggerUI ASP.Net WebAPI2

來源:互聯網
上載者:User

標籤:設定檔   檔案夾   return   程式   控制台   

目前在用ASP.NET的 WebAPI2來做後台介面開發,在與前台做測試的時候,總是需要發送一個demo給他,但是這樣很麻煩的,他還有可能記不住。

然後就想到SwaggerUI 產生測試文檔。

話不多說,來看看它的具體用法吧!

第一步:

在VS2015(後者是你自己的工具裡)開啟程式包管理主控台輸入:

Install-Package Swashbuckle

等待安裝,完成後會發現,在App_Start檔案夾下會多出來一個設定檔SwaggerConfig.cs

650) this.width=650;" src="http://images2015.cnblogs.com/blog/756707/201702/756707-20170220165729007-172291445.png" style="margin:0px;padding:0px;border:none;" />

第二步:

開啟這個檔案然後找到Register()方法裡的c.IncludeXmlComments(),增加一個參數,也就是說改成c.IncludeXmlComments(GetXmlCommentsPath(thisAssembly.GetName().Name));

然後在跟Register方法平級的地方增加一個方法

protected static string GetXmlCommentsPath(string name)        {            return string.Format(@"{0}\bin\{1}.XML", AppDomain.CurrentDomain.BaseDirectory, name);        }

然後設定檔到此結束,

第三步:

繼續做對於項目的配置

650) this.width=650;" src="http://images2015.cnblogs.com/blog/756707/201702/756707-20170220170353757-1830263459.png" style="margin:0px;padding:0px;border:none;" />

右鍵項目->屬性->產生->勾選(XML文檔檔案)->點擊儲存檔案即可。

第四步:

輸入訪問路徑

http://localhost:15416/swagger/ui/index

這裡邊關於主機和連接埠需要替換成寄幾的。

最後成果

650) this.width=650;" src="http://images2015.cnblogs.com/blog/756707/201702/756707-20170220212111226-1110209949.png" style="margin:0px;padding:0px;border:none;" />

這裡做個備忘,發現每個請求後邊都會有注釋,而且點開以後也有注釋,這個就是你在你的介面中的注釋,它可以直接拿過來顯示的。

650) this.width=650;" src="http://images2015.cnblogs.com/blog/756707/201702/756707-20170221092548898-1093429804.png" style="margin:0px;padding:0px;border:none;" />

/// <summary>/// 發送手機驗證碼/// </summary>/// <param name="mobile">手機號</param>


SwaggerUI ASP.Net WebAPI2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.