在.NetCore2.0中使用Swagger
一、新建.netCore webapi项目
二、安装swagger ,通过 Package Manager 控制台:Install-Package SwashBuckle.AspNetCore -Pre
三、修改Startup.cs
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Swashbuckle.AspNetCore.Swagger; using Microsoft.Extensions.PlatformAbstractions; using System.IO; namespace CoreApi { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddSwaggerGen(c=> { c.SwaggerDoc("v1", new Info { Version="v1", Title="Demo Api" }); var basePath = PlatformServices.Default.Application.ApplicationBasePath; var xmlPath = Path.Combine(basePath, "CoreApi.xml"); c.IncludeXmlComments(xmlPath); } ); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseSwagger(); app.UseSwaggerUI(c=>{ c.SwaggerEndpoint("/swagger/v1/swagger.json", "DemoApi"); }); app.UseMvc(); } } }
四、修改项目属性,生成xml文档
五、launchsettings.json文件,设置启动页。
六、效果