ASP.NET Web API Documentation Generator - Use Swagger

Swagger is a set of interactive API documentation generator, using HTML and Javascript written, unlike the previously described ASP.NET Web API Help Page is, Swagger is a set of Open Source Software, now supports many of the REST API, the reason that this is an interactive document, in addition to displaying the output of the API specification, but also allows the user to operate in real-time on Swagger UI interface, you can immediately see the results.

This is a brief description of how to add Swagger will function in a ASP.NET Web API project.



Swagger

http://swagger.io/

Swagger if not quite know what features and can do something, I can go to the official website of the recommended Live Demo site to see, look, the point that operating several functions, should be able to know,

http://petstore.swagger.io/

Installation in ASP.NET Web API project in

So how to use ASP.NET Web API project in it?

In fact, you can not install from scratch so hard Swagger, someone has already developed a good NuGet kit, just install from NuGet to ASP.NET Web API project, and then slightly modified it, the basic installation is not very complicated, just follow the following steps make it.

Examples follow before a "ASP.NET Web Api - Help Page" article of example, in the project through the installation of two Packages NuGet, namely: Swashbuckle and Swashbuckle.Core

Swashbuckle installed will be installed along the Swashbuckle.Core come in,

After installing Swashbuckle and Swashbuckle.Core, to check to see if the following files are not established there,

App_Start/SwaggerConfig.cs

一定要做的就是别忘了 Controller 与 Action 方法要加上 Summary

另外千万别忘了在项目属性里要勾选建置时输出“XML 文档文件”

再来最重要的就是修改 SwaggerConfig.cs 的内容,在程序的第 99 行,将这一行给反注解,

不过把这一行给反注解之后却会出现错误,

这是因为还没有实践 GetXmlCommentsPath() 方法,这个方法是要提供 XML Document 文件的路径,这么一来 Swagger 才能够正确的显示 Controller 与 Action 方法的相关资讯,

万事具备之后就可以执行网站了,要查看 API 服务的 Swagger 文档页面,在网址根目录后面加上 Swagger 就可看到,例如:http://localhost:60900/Swagger

线上执行后显示回传结果


这一篇只是做简单的介绍,如果你有兴趣可以在进阶研究 Swagger,但因为我们是开发 ASP.NET Web Api 并且是使用 Swagger for Web API - Swashbuckle,所以建议各位要进阶研究的对象应该是“Swashbuckle”,其实有很多很进阶的修改与设定可以玩的。

Postman 与 Swagger 的差异

  • Postman 适合开发人员的统整管理,并且可以直接导出 C# (RestSharp) 的程序,并且直接放在程序里使用
  • Swagger 适合即时开发的使用,甚至是提供给非开发人员测试使用
  • 建议两种同时使用,开发人员在开发时的测试可以使用 Swagger 马上做测试,完成开发后可以到 Postman 之后去对系统做测试

 

参考连结

http://swagger.io/

http://petstore.swagger.io/

https://github.com/domaindrivendev/Swashbuckle

KingKong Bruce记事: ASP.NET Web API 文档产生器(2) - Swagger

以上

分享

原文:大专栏  ASP.NET Web API 文档产生器 - 使用 Swagger


Guess you like

Origin www.cnblogs.com/chinatrump/p/11516344.html