使用Swashbuckle.AspNetCore生成.NetCore WEBAPI的接口文档

2021-05-23 12:29

阅读:761

一、问题

  使用Swashbuckle.AspNetCore生成.NetCore WEBAPI的接口文档的方法

二、解决方案

  参考文章:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs=visual-studio

  1、Install-Package Swashbuckle.AspNetCore

  2、在ConfigureServices中添加AddSwaggerGen。

技术图片
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext(opt => opt.UseInMemoryDatabase("TodoList"));
    services.AddMvc();

    // Register the Swagger generator, defining one or more Swagger documents
      services.AddSwaggerGen(options =>
      {
                //options.DescribeAllEnumsAsStrings();
                options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
                {
                    Title = "Azure Adapter Api - Catalog HTTP API",
                    Version = "v1",
                    Description = "The Catalog Microservice HTTP API. This is a Data-Driven/CRUD microservice sample",
                    TermsOfService = "Terms Of Service"
               });
      });
}
技术图片

  3、在Configure中添加

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

三、运行测试:

  输入 http://127.0.0.1:8080/swagger/  或者 http://127.0.0.1:8080/swagger/v1/swagger.json

  技术图片

 


评论


亲,登录后才可以留言!