In the previous article, I introduced some of the basic features of Swagger-ui , and here's how to integrate Swagger-ui in Webapi today . Here is an example of a simple CRUD REST service.
1 /// <summary>2 ///User Management3 /// </summary>4 Public classUsercontroller:apicontroller5 {6 StaticList<user> _users =NewList<controllers.user>();7 8 Static int_idseed =0;9 Ten One /// <summary> A ///Add User - /// </summary> - /// <param name= "user" >User Data</param> the /// <returns></returns> - Publicuser Post (user user) - { -User. Id = + +_idseed; + _users. ADD (user); - + returnuser; A } at - /// <summary> - ///Modify User - /// </summary> - /// <param name= "id" >User Number</param> - /// <param name= "user" >New User Information</param> in /// <returns></returns> - PublicIhttpactionresult Put (intID, user user) to { + varCurrent =Get (ID); - if(Current = =NULL) the returnNotFound (); * $User. Id =Current . Id;Panax Notoginseng _users. Remove (current); - _users. ADD (user); the + returnOk (); A } the + /// <summary> - ///Delete User $ /// </summary> $ /// <param name= "id" >User Number</param> - Public voidDelete (intID) - { the varCurrent =Get (ID); - _users. Remove (current);Wuyi } the - /// <summary> Wu ///get a list of users - /// </summary> About /// <returns>FFF</returns> $ PublicIenumerable<user>GetAll () - { - return_users; - } A + /// <summary> the ///gets the specified user - /// </summary> $ /// <param name= "id" >numbering</param> the /// <returns></returns> the PublicUser Get (intID) the { the return_users. Find (i = i.id = =ID); - } in } the the About /// <summary> the ///User the /// </summary> the Public classUser + { - /// <summary> the ///numberingBayi /// </summary> the Public intId {Get;Set; } the - /// <summary> - ///name the /// </summary> the Public stringName {Get;Set; } the the /// <summary> - ///Address the /// </summary> the Public stringAddress {Get;Set; } the}View Code
Use Swashbuckle Integrated Swagger-ui
Swagger-ui itself only provides online testing capabilities, to integrate it also needs to tell it the various service and parameter information provided by this project. There is a lot of work to do here, but fortunately many third-party libraries have done this for us. I'm using Swashbuckle, andit's easier to use it to add its packages directly with Nuget:
Pm> Install-package Swashbuckle
When the package is added, it will itself add some of its corresponding registered code to the project, although we can not care about these operations, but sometimes we need to modify some of the relevant configuration.
Recompile and run the project, we can add a "/swagger" to the project address to access the Swagger test page.
At this point, you can do the API test operation. However, the method described here applies only to IIS-hosted WEBAPI projects, and there is some extra work to do with self host or Owin-hosted methods, see Swashbuckle's home page documentation
Modify Title
The default title is the project name (I am Here "WebApplication1"), we often need to change it to a more friendly name, modified this operation in the previous SwaggerConfig.cs file, find the following code:
The previous parameter is Swagger-ui version, as if currently support V1 and V2 two kinds, followed by the title, directly modified.
Integrated XML Notes
The previous test page works fine, but the interface and parameter descriptions are empty and unfriendly, but these can actually be read from the code's XML annotations. The following two steps are required to achieve this effect:
First, open the output of the annotated XML document:
Then, in SwaggerConfig.cs's Enableswagger callback function function, use the Includexmlcomments function to specify the path to the XML document.
In this way, our test page can display various annotations (seemingly unsupported by the controller's description).
More advanced operations here is not introduced, the need for friends can refer to Swashbuckle's homepage documentation: Https://github.com/domaindrivendev/Swashbuckle
Add test page to Webapi's Rest Interface (ii)