1 创建一个asp.net core 项目 3.1
先导入Swashbuckle.AspNetCore 包
在 Startup 中添加
#region Swagger
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1.1.0",
Title = "Swagger API",
Description = "描述"
});
// 为 Swagger JSON and UI设置xml文档注释路径
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, $"{typeof(Startup).Assembly.GetName().Name}.xml"), true);
//options.IncludeXmlComments(xmlPath);
});
#endregion
#region Swagger
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/V1/swagger.json", "ApiHelp V1");
//如果设置根目录为swagger,将此值置空
options.RoutePrefix = string.Empty;
});
#endregion 注意 必须要大写的 V 就是/swagger/V1/swagger.json 不然会报/swagger/V1/swagger.json未定义
要把项目 生成 XML文档文件 勾选
把xml文件设置成始终复制
效果图如下
xxx/swagger/index.html 或者 直接xxx/index.html 就能看到文档