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

原文地址:https://www.cnblogs.com/qy1234/p/12851698.html