Automatic generation of API documentation, swagger configuration

Source: Internet
Author: User

First step: Referencing assemblies

Open the NuGet Package Manager, search for swagger, install the first, notice where the circles are circled,

The main program and UI are already included, and SwaggerConfig.cs will be generated under the root directory App_start folder when the installation is complete.

Step Two: Configuration information

Follow the configuration below and note the namespace.

1 usingSystem.Web.Http;2 usingWebactivatorex;3 usingDemo.api;4 usingswashbuckle.application;5 usingSwashbuckle.swagger;6 usingSystem;7 usingSystem.Collections.Concurrent;8 usingSystem.Collections.Generic;9 usingSystem.Xml;Ten usingSystem.IO; One  A[Assembly:preapplicationstartmethod (typeof(Swaggerconfig),"Register")] -  - namespaceDemo.api the { -      Public classSwaggerconfig -     { -          Public Static voidRegister () +         { -             varthisassembly =typeof(Swaggerconfig). Assembly; +  A globalconfiguration.configuration at. Enableswagger (c = -                { -C.singleapiversion ("v1","Demo.api"); -C.includexmlcomments (System.AppDomain.CurrentDomain.BaseDirectory +"/bin/demo.api.xml"); -C.customprovider ((defaultprovider) =NewCachingswaggerprovider (defaultprovider)); -                }) in. Enableswaggerui (c = -                { to                    //path rules, project namespaces. Folder name. js file name +C.injectjavascript (thisassembly,"Demo.API.Scripts.swagger.js"); -                }); the         } *     } $ Panax Notoginseng      Public classCachingswaggerprovider:iswaggerprovider -     { the         Private Staticconcurrentdictionary<string, swaggerdocument> _cache = +                  Newconcurrentdictionary<string, swaggerdocument>(); A  the         Private ReadOnlyIswaggerprovider _swaggerprovider; +  -          PublicCachingswaggerprovider (Iswaggerprovider swaggerprovider) $         { $_swaggerprovider =Swaggerprovider; -         } -  the          PublicSwaggerdocument Getswagger (stringRooturl,stringapiversion) -         {Wuyi             varCacheKey =string. Format ("{0}_{1}", Rooturl, apiversion); theSwaggerdocument Srcdoc =NULL; -             //Read Only once Wu             if(!_cache. TryGetValue (CacheKey, outsrcdoc)) -             { AboutSrcdoc =_swaggerprovider.getswagger (Rooturl, apiversion); $  -Srcdoc.vendorextensions =Newdictionary<string,Object> {{"Controllerdesc", Getcontrollerdesc ()}}; - _cache. TryAdd (CacheKey, srcdoc); -             } A             returnSrcdoc; +         } the  -         /// <summary> $         ///Read the controller description from the API documentation the         /// </summary> the         /// <returns>All controller Descriptions</returns> the          Public Staticconcurrentdictionary<string,string>Getcontrollerdesc () the         { -             stringXmlpath =string. Format ("{0}/bin/demo.api.xml", System.AppDomain.CurrentDomain.BaseDirectory); inconcurrentdictionary<string,string> dic =Newconcurrentdictionary<string,string>(); the             if(!file.exists (xmlpath)) the             { About                 returndic; the             } theXmlDocument doc =NewXmlDocument (); the Doc. Load (Xmlpath); +XmlNodeList list = doc. SelectNodes ("//member[contains (@name, ' T: ')]"); -             foreach(XmlNode Iteminchlist) the             {Bayi                 stringval = Item. attributes["name"]. Value; the                 stringKey = val. Substring (Val. LastIndexOf ('.') +1). Replace ("Controller",""); theXmlNode node =item. FirstChild; -                 if(!DiC. ContainsKey (key)) -                 { the DiC. TryAdd (Key, node. Innertext.trim ()); the                 } the             } the             returndic; -         } the  the     } the}
View Code

Step three: Output XML file

Right-click the item's properties, generate, tick

Fourth step: Show controller Notes

Build a JS file yourself, and then add it to the SwaggerConfig.cs, as in the code above

C.injectjavascript (thisassembly, "Demo.API.Scripts.swagger.js");

$(function () {    varinit =function() {$.getjson ($ ("#input_baseUrl"). Val (), {},function(RES) {$ ("#resources_container. Resource"). each (function(I, item) {var_id = $ (item). attr ("id"); if(!_id) {return; } varStrsummary = Res. Controllerdesc[_id.substring (9)]; if(strsummary) {$ (item). Children (". Heading"). Children (". Options"). Prepend (' <li style= ' color: #000 ' > ' + strsummary + ' </li> ');        }            });    });    }; Init ();});

Add the JS, CSS and other files Ah, need to set up, or will error

Select the file, right-click Property, and the build action to the embedded resource.

Chinese will not need, your notes are Chinese on the line, there are not a few words.

Access address is a domain name or ip+/swagger/ui/index

Automatic generation of API documentation, swagger configuration

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.