.net core 中如何使用swagger?

**1.ConfigureServices方法中注册服务**

services.AddSwaggerGen(c =>
{
  c.SwaggerDoc("v1", new OpenApiInfo
  {
    Version = "v1",
    Title = "Bingle API",
    Description = "一个简单的ASP.NET Core Web API",
    TermsOfService = new Uri("https://www.cnblogs.com/niguang"),
    Contact = new OpenApiContact
    {
      Name = "bingle",
      Email = string.Empty,
      Url = new Uri("https://www.cnblogs.com/niguang/"),
    },
    License = new OpenApiLicense
    {
      Name = "许可证",
      Url = new Uri("https://www.cnblogs.com/niguang/"),
    }
  });
  //为 Swagger JSON and UI设置xml文档注释路径
  var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
  var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
  c.IncludeXmlComments(xmlPath);
});

**2.Configure中启用服务**

app.UseSwagger();
app.UseSwaggerUI(c =>
{
  c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});

3.设置:选中项目--属性--生成--输出选项卡勾选“XML文档文件”

4.配置完成,运行查看即可:https://localhost:44396/swagger/index.html

posted @ 2020-06-01 14:50  逆光迷矢  阅读(272)  评论(0编辑  收藏  举报