一、在Startup中进行配置以及调用
#region swagger接口帮助文档 在ConfigureServices中注册服务 services.AddHttpClient(); services.AddSingleton<MessageHandleService>(); services.AddHostedService<MessageFileSaveService>(); if (configuration["tencentIM:syncMessage"] == "1") { services.AddHostedService<MessageManageService>(); } services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "接口名称-自定义", Version = "v1.0", Description = "框架说明文档" } ); // 为 Swagger 设置xml文档注释路径 var basePath = AppContext.BaseDirectory; //var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
//当前接口目录下生成该文件 binDebug etcoreapp3.1MyDemo.WebApi.xml
var xmlPath = Path.Combine(basePath, "MyDemo.WebApi.xml");
if (File.Exists(xmlPath)) { c.IncludeXmlComments(xmlPath, true); } //当前接口目录下生成该文件 binDebug etcoreapp3.1MyDemo.Data.xml
var xmlModelPath = Path.Combine(basePath, "MyDemo.Data.xml");
if (File.Exists(xmlModelPath))
{
c.IncludeXmlComments(xmlModelPath);
}
});
}
#endregion
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { } app.UseRouting();
//将中间件添加到管道中
app.UseSwagger(); app.UseSwaggerUI(option => { option.SwaggerEndpoint("/swagger/v1/swagger.json", "MyDemo-API"); }); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
二、配置XML文档的输出路径-右击项目-生成标志