第一種方法:直接添加swagger,會在app_start中生成SwaggerConfig,在該文件中配置相關內容(把生成xml打開);json
第二種方法:添加四個引用文件Swashbuckle.AspNetCore.Swagger 、Swashbuckle.AspNetCore.SwaggerGen、Swashbuckle.AspNetCore.SwaggerUI、Swashbuckle.AspNetCore;而後在startup文件中添加如下代碼:app
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSwaggerGen(c => { c.SwaggerDoc("v1",new Microsoft.OpenApi.Models.OpenApiInfo { Title = "myApi", Version = "v1" }); var xmlFile = $"{Assembly.GetEntryAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); }); services.AddMvcCore().AddApiExplorer();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHsts(); } app.UseHttpsRedirection(); app.UseMvc(); app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "myApi"); }); }