ASP. NET WEBAPI Swagger Ultimate Build

Source: Internet
Author: User

about why use swagger

At present, a little bit of the size of the company, has been developed from the original waterfall flow to agile development, the implementation of the front-end separation, for the next-end engineers only focus on writing API, the programmer is most annoying is to write API documents, and therefore produced a swagger.

Swagger principle

Swagger is the use of reflection technology to traverse all API interfaces, and read comments from the XML file, in the use of swagger built-in template combination HTML display to the client implementation interface visualization, and can be called.

ASP. WEBAPI Swagger Integration

1:vs2017, create a new Web project, select Webapi

2: Delete views, Scripts, Models, fonts, content, areas directory

3: Delete RouteConfig.cs, FilterConfig.cs, BundleConfig.cs

4: Delete HomeController.cs

Removing exception codes in 5:global.asax

6:nuget Search Swagger, install Swashbuckle,swagger.net.ui

7: Right-click Project-"Properties--" Generate--"output-" Check the XML document file--"Save

8: Modify SwaggerConfig.cs

New method, release C. Includexmlcomments (Getxmlcommentspath ()); Comments (note: For example, the return value is an object and then not the same item, you need to call it multiple times)

Private Static string Getxmlcommentspath () {      eturn System.String.Format (@ "{0}\bin\{project name}. XML",                System.AppDomain.CurrentDomain.BaseDirectory);}

9: Right-click Project nuget--"Installed-" search swagger, uninstall swagger.net.ui--"option check forced uninstall, click Uninstall. Uninstalling Swagger.net

10: Then in the URL address: For example: Http://localhost:port/swagger can

  

Swagger Advanced

1: When there is a DTO project, the DTO also needs to call the comments to the client, note that the DTO project also refer to the 7th generation of XML file, copy the 8th method

2:swagger added header information, add in the above comment: C. Operationfilter

 Public classHttpheaderfilter:ioperationfilter { Public voidApply (Operation operation, Schemaregistry Schemaregistry, apidescription apidescription) { if(Operation.parameters = =NULL) Operation.parameters=NewList<parameter>(); varFilterpipeline = ApiDescription.ActionDescriptor.GetFilterPipeline ();//To determine whether to add a permission filter                varisauthorized = Filterpipeline.select (FilterInfo =filterinfo.instance). Any (filter= Filter isErpfilterattribute);//determine whether anonymous methods are allowed//var allowanonymous = apidescription.actiondescriptor.getcustomattributes<allowanonymousattribute> (). Any ();                if(isauthorized) {OPERATION.PARAMETERS.ADD (NewParameter {Name="AppId", @in="Header", Description="App ID (institution number)", Required=false, type="string"                    }); OPERATION.PARAMETERS.ADD (NewParameter {Name="Version", @in="Header", Description="Version number", Required=false, type="string"                    }); OPERATION.PARAMETERS.ADD (NewParameter {Name="Ts", @in="Header", Description="time Stamp", Required=false, type="string"                    }); OPERATION.PARAMETERS.ADD (NewParameter {Name="Lang", @in="Header", Description="Language Packs", Required=false, type="string"                    }); OPERATION.PARAMETERS.ADD (NewParameter {Name=" Sign", @in="Header", Description="Signature", Required=false, type="string"                    }); return; }            }        }
View Code

3: Usage of annotations

The use of annotations, in the API interface "///" Three slash comment summary for Interface name comments, summary enter <remarks> for notes, note that each field comments must be comprehensive, otherwise you cannot display the full

The reference code is as follows

/// <summary>        ///page List of roles/// </summary>        /// <remarks>        ///Code return value Description:///         ///error code address:http://xxxx.com/        ///         /// </remarks>        /// <param name= "Conditionmodel" >Paging Query Criteria</param>        /// <returns></returns>[HttpGet, Route ("Api/clientroles")]         PublicOutputmodel<pagingoutputmodel<list<baseroledto>>>getrolelist ([Fromuri]paginginputmodel conditionmodel) {return NULL;}
View Code

Picture Show

ASP. NET WEBAPI Swagger Ultimate Build

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.