https://www.nuget.org/packages/Swashbuckle.AspNetCore.SwaggerGen/
1.添加NUGET包 Swashbuckle.AspNetCore:
現在最新版是2.4 (ps:個人覺得界面比1.0醜)
2.startup中配置:
ConfigureServices:
//註冊swagger生成器,定義一個或多個swagger文檔 services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Version = "v1", Title = "測試 API", Description = "A simple example ASP.NET Core Web API", TermsOfService = "None", Contact = new Contact { Name = "ck", Email = "", Url = "http://www.cnblogs.com/chuankang/" }, License = new License { Name = "博客園", Url = "http://www.cnblogs.com/chuankang/" } }); // api描述文檔xml的生成地址和文件名,需要在項目的屬性中進行配置 var basePath = AppContext.BaseDirectory; var xmlPath = Path.Combine(basePath, "SwaggerDemo.xml"); c.IncludeXmlComments(xmlPath); });
Configure中:
// 啓用中間件以生成JSON作爲JSON端點. app.UseSwagger(); // 啓用中間件以提供用戶界面(HTML、js、CSS等),特別是指定JSON端點。 app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); });
statrup類:
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Swashbuckle.AspNetCore.Swagger; using System; using System.IO; namespace AspNetCoreMVC { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. 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 { Version = "v1", Title = "測試 API", Description = "A simple example ASP.NET Core Web API", TermsOfService = "None", Contact = new Contact { Name = "Shayne Boyer", Email = "", Url = "https://twitter.com/spboyer" }, License = new License { Name = "Use under LICX", Url = "https://example.com/license" } }); // Set the comments path for the Swagger JSON and UI. var basePath = AppContext.BaseDirectory; var xmlPath = Path.Combine(basePath, "SwaggerDemo.xml"); c.IncludeXmlComments(xmlPath); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseBrowserLink(); app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } // 啓用中間件以生成JSON作爲JSON端點. app.UseSwagger(); // 啓用中間件以提供用戶界面(HTML、js、CSS等),特別是指定JSON端點。 app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); }); app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } } }
api描述文檔xml的生成地址和文件名,需要在項目的屬性中進行配置,右鍵項目-》屬性-》生成 如下圖:
加上1591忽略屬性類名必須加xml註釋
3.在controller中action方法都要指定http請求Post(新增),Put(修改),Delete(刪除),Get(查詢)中的一個,不然會報錯:
http://localhost:55642/swagger/v1/swagger.json 查看錯誤原因
[Route("Home")] 改爲 [Produces("application/json")]
[HttpPost] 改爲
[HttpPost]
[Route("TestApi")]
或者改爲 [HttpPost("TestApi")]
4.啓動看效果:
5.每次輸入/swagger太麻煩,可以設置藉助vs進行跳轉,如下圖加上 "launchUrl": "swagger",:
參考文檔:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/web-api-help-pages-using-swagger
https://github.com/domaindrivendev/Swashbuckle.AspNetCore