本文包括兩個部分:
- webapi中使用swagger
- 修改webapi的路由和默認參數
- WebApi中使用swagger
-
項目打開之后,選擇 引用,右鍵,管理NuGet程序包
-
瀏覽,搜索swagger,選擇第一個swashbuckle,安裝
-
安裝好之后,右鍵項目,選擇屬性,生成,在下面的輸出那里勾選:XML文檔文件,如果沒有自動填充好路徑,需要自己填寫一下,文件名可以自己
-
打開App_Start文件夾下的SwaggerConfig.cs文件,新增一個如下方法:
-
private static string GetXmlCommentsPath()
{
return System.String.Format(@"{0}\bin\Coldairarrow.WebApi.xml", System.AppDomain.CurrentDomain.BaseDirectory);
} -
搜索GetXmlCommentsPath,下面能搜到已經注釋了,自己把注釋放開,要是沒搜到,就自己手動寫一下c.IncludeXmlComments(GetXmlCommentsPath());注意要寫在register方法里面
-
運行項目,在根路徑后面直接加swagger,就會自動跳轉到文檔,如:http://localhost:13520/swagger/ui/index,能看到我們寫的一些注釋