1、创建一个.netcore Mvc项目
2、安装相关NuGet包
3、在 Startup.cs 中的 ConfigureServices 下添加
var basePath = ApplicationEnvironment.ApplicationBasePath;
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("V1", new OpenApiInfo
{
// {ApiName} 定义成全局变量,方便修改
Version = "V1",
Title = $"{ApiName} 接口文档——Netcore 3.0",
Description = $"{ApiName} HTTP API V1",
Contact = new OpenApiContact { Name = ApiName, Email = "Blog.Core@xxx.com", Url = new Uri("https://www.jianshu.com/u/94102b59cc2a") },
License = new OpenApiLicense { Name = ApiName, Url = new Uri("https://www.jianshu.com/u/94102b59cc2a") }
});
c.OrderActionsBy(o => o.RelativePath);
var xmlPath = Path.Combine(basePath, "WebApplication2881.xml");//这个就是刚刚配置的xml文件名
c.IncludeXmlComments(xmlPath, true);//默认的第二个参数是false,这个是controller的注释,记得修改
});
4、在 Startup.cs 中的 Configures添加
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"/swagger/V1/swagger.json", $"{ApiName} V1");
//路径配置,设置为空,表示直接在根域名(localhost:8001)访问该文件,注意localhost:8001/swagger是访问不到的,去launchSettings.json把launchUrl去掉,如果你想换一个路径,直接写名字即可,比如直接写c.RoutePrefix = "doc";
c.RoutePrefix = "";
});
5、配置注意文件及注释警告忽略问题
6,最后查看效果,
7,隐藏某些接口
[ApiExplorerSettings(IgnoreApi = true)]