第一种:安装nuget插件NSwag.AspNetCore,然后在start.cs文件中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public void ConfigureServices(IServiceCollection services) { //... services.AddSwaggerDocument(); //... } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseAuthentication(); //添加如下 app.UseSwagger(); app.UseSwaggerUi3(); app.UseMvc(); } |
然后启动:http://localhost:端口/swagger/
第二种办法:nuget安装Swashbuckle.AspNetCore
1 2 3 4 |
services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" }); }); |
1 2 3 4 5 6 7 8 9 10 11 |
app.UseAuthentication(); // 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"); }); |
发表评论