OWIN WebAPI如何自動生成接口文檔

1.爲什麼要OWIN WebAPI自動生成接口文檔

OWIN WebAPI項目使用Swagger UI自動生成接口文檔,不需要頻繁更新接口文檔,保證接口文檔與代碼的一致,方便對接接口或者測試接口。

2. Swagger UI

Swagger UI:提供了一個可視化的UI頁面展示描述文件。接口的調用方、測試、項目經理等都可以在該頁面中對相關接口進行查閱和做一些簡單的接口請求。該項目支持在線導入描述文件和本地部署UI項目。

3.自定義生成Swagger文檔

namespace MaterialSystem.API
{
    public class Startup
    {
        public void Configuration(IAppBuilder appBuilder)
        {
            // Configure Web API for self-host. 
            HttpConfiguration config = new HttpConfiguration();

            config.MapHttpAttributeRoutes();//添加路由路徑
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );


            appBuilder.UseWebApi(config);

//Swagger接口文檔
            config
     .EnableSwagger(c =>
     {
         c.SingleApiVersion("v1", "A title for your API");
         c.IncludeXmlComments(GetXMLDocumentPath());
     })
     .EnableSwaggerUi();        
        }

        private string GetXMLDocumentPath()
        {
            return AppDomain.CurrentDomain.BaseDirectory + "MaterialSystem.API.xml";
        }
    }
}

4. Swagger的實例

1.查看API文檔,,如圖所示

2.測試用戶登錄API

5.參考資料

網址:https://github.com/domaindrivendev/Swashbuckle

Web API Tools - Owin Self-Host & Swagger:https://www.youtube.com/watch?v=9h2tlxrPPyc

 

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