傳統的API從開發測試開始咱們常常借用相似Postman、fiddle等等去作接口測試等等工具;Swagger 爲API的在線測試、在線文檔提供了一個新的簡便的解決方案;html
一、引用包web
二、api項目屬性json
三、修改swaggernet.csapi
NET使用:Swagger-Netrestful
引用NuGet包:Swashbuckle.AspNetCore ;app
包含以下部分:dom
Swashbuckle.AspNetCore.Swagger: 一些模型實體定義
Swashbuckle.AspNetCore.SwaggerGen: Swagger生成器
Swashbuckle.AspNetCore.SwaggerUI:Swagger UI工具工具
配置:startup.csvisual-studio
注入swagger:測試
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); // Register the Swagger generator, defining one or more Swagger documents services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" }); }); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // Enable middleware to serve generated Swagger as a JSON endpoint. app.UseSwagger(); // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint. app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); }); app.UseMvc(); }
編譯運行,下面兩個網址:
API文檔界面:http://localhost:<random_port>/swagger
API的restful說明json文件:http://localhost:<random_port>/swagger/v1/swagger.json
文檔:
博客資源:http://www.cnblogs.com/dotNETCoreSG/p/aspnetcore_02-09_web-api-help-pages-using-swagger.html