问题:
一个项目里会有比较多的WebApi,不想放到一个测试页面中,希望可以分组。
解决:StartUP.cs
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); // 注册Swagger服务 services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "My Demo API", Version = "v1.0", Description = "My Demo API" } ); c.SwaggerDoc("v2", new OpenApiInfo { Version = "v1.0", Title = "基础接口", Description = "用户登录认证接口" }); ////c.IncludeXmlComments(System.IO.Path.Combine(System.AppContext.BaseDirectory, "ZhiKeCore.API.xml")); }); }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); app.UseSwagger(); app.UseSwaggerUI(c => { //c.SwaggerEndpoint("/swagger/v1.0/swagger.json", "My Demo API (V 1.0)"); c.SwaggerEndpoint("/swagger/v1/swagger.json", "My Demo API"); c.SwaggerEndpoint("/swagger/v2/swagger.json", "设备集成接口"); }); }
写具体的WebApi需设置分组:以下代码表示设置在v2设备集成接口那个测试页中
[ApiExplorerSettings(GroupName = "v2")]
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
效果见下图: