赞
踩
IGeekFan.AspNetCore.Knife4jUI
Swashbuckle.AspNetCore.Swagger
Swashbuckle.AspNetCore.SwaggerGen
<PackageReference Include="IGeekFan.AspNetCore.Knife4jUI" Version="0.0.11" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.3.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.3.1" />
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "API V1", Version = "v1" });
c.AddServer(new OpenApiServer()
{
Url = "",
Description = "vvv"
});
c.CustomOperationIds(apiDesc =>
{
var controllerAction = apiDesc.ActionDescriptor as ControllerActionDescriptor;
return controllerAction.ControllerName + "-" + controllerAction.ActionName;
});
c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "SwaggerDemo.xml"), true);
});
// 将 UseSwaggerUI 替换成 UseKnife4UI app.UseKnife4UI(c => { c.RoutePrefix = string.Empty; // serve the UI at root c.SwaggerEndpoint("/v1/api-docs", "V1 Docs"); }); app.UseRouting(); // 路由中间件一定要添加 app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapSwagger("{documentName}/api-docs"); });
启动生成API文档,否则启动会报错:
“找不到xxx.xml文件”
如下图所示
“SwaggerDemo.xml” 文件名称与代码中保持一致
更改 Properties 文件夹下的 launchSettings.json 文件中的
launchUrl
节点 将swagger
替换为index.html#/home
启动时便可以直接打开文档啦
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。