Github:https://github.com/domaindrivendev/ahoy
Previous articles have introduced the use of swagger to generate documents in ASP. ASP. NET Core 1.0 is also supported.
Dependency Packages
"Dependencies": { "Swashbuckle.swaggergen": "6.0.0-rc1-final", "Swashbuckle.swaggerui": "6.0.0- Rc1-final " }
Project Properties Select the Generate output on Build option to generate an XML comment
Startup class
Public classStartup {//Private readonly string pathxml = @ "\\Mac\Home\Desktop\asp.net core\learn_asp.net core 1.0\artifacts\bin\ Swaggerforasp.netcore\debug\dnx451\swaggerforasp.netcore1.0.xml "; Private ReadOnly stringPathxml =@"C:\Users\irving\Desktop\asp.net core\learningasp.netcore\artifacts\bin\learningasp.netcore\debug\dnx451\ LearningASP.NETCore.xml"; PublicStartup (ihostingenvironment env) {//Set up configuration sources. varBuilder =NewConfigurationbuilder (). Addjsonfile ("Appsettings.json") . Addenvironmentvariables (); Configuration=Builder. Build (); } PublicIconfigurationroot Configuration {Get;Set; } //This method gets called by the runtime. Use this method to add services to the container. Public voidconfigureservices (iservicecollection services) {//ADD Framework Services.Services. Addmvc (); Services. Addswaggergen (); Services. Configureswaggerdocument (Options={options. Singleapiversion (NewInfo {License=NewLicense {Name ="Irving", URL =@"http://cnblogs.com/irving"}, contact=NewContact {Name ="Irving", Email ="[email protected]", URL =@"http://cnblogs.com/irving"}, Version="v1", Title="ASP. NET Core 1.0 WebAPI", Description="A simple for ASP. NET Core 1.0 WebAPI", Termsofservice="None" }); Options. Operationfilter (Newapplyxmlactioncomments (pathxml)); }); Services. Configureswaggerschema (Options={options. Ignoreobsoleteproperties=true; Options. Describeallenumsasstrings=true; Options. Modelfilter (Newapplyxmltypecomments (pathxml)); }); } //This method gets called by the runtime. Use this method to configure the HTTP request pipeline. Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {Loggerf Actory. Addconsole (Configuration.getsection ("Logging")); Loggerfactory.adddebug (); if(env. Isdevelopment ()) {app. Usebrowserlink (); App. Usedeveloperexceptionpage (); } Else{app. Useexceptionhandler ("/home/error"); } app. Useiisplatformhandler (); App. Usestaticfiles (); App. USEMVC (Routes={routes. MapRoute (Name:"default", Template:"{controller=home}/{action=index}/{id?}"); }); App. Useswaggergen (); App. Useswaggerui (); } //Entry point for the application. Public Static voidMain (string[] args) = webapplication.run<startup>(args); }
Orderscontroller class
[Route ("api/orders")] Public classOrderscontroller:controller {[HttpGet] [Route ("Info")] Public AsyncTask<actionresult>Info () {return awaitTask.run (() = { returnJson (New{name ="Irving", age = - }); }). ContinueWith (t=T.result); } //get:api/values[HttpGet] Publicienumerable<string>Get () {return New string[] {"value1","value2" }; } //GET API/VALUES/5[HttpGet ("{ID}")] Public stringGet (intID) {return "value"; } //POST api/values[HttpPost] Public voidPost ([Frombody]stringvalue) { } //PUT API/VALUES/5[Httpput ("{ID}")] Public voidPut (intID, [frombody]stringvalue) { } //DELETE API/VALUES/5[Httpdelete ("{ID}")] Public voidDelete (intID) {}}
Visit: http://localhost/swagger/ui/index.html
Refer:
http://damienbod.com/2015/12/13/asp-net-5-mvc-6-api-documentation-using-swagger/
Using Swagger to generate documents in ASP. NET Core 1.0