.netcore Swagger 生成 api接口文档

1, 引用第三方包,

Swashbuckle.AspNetCore

Swashbuckle.AspNetCore.Swagger

Swashbuckle.AspNetCore.SwaggerUI

最简单方法复制:

    <PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="4.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="4.0.1" />

2. startup.cs

    //swagger
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = typeof(Startup).GetTypeInfo().Assembly.GetName().Name });
});

3

app.UseSwagger().UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", typeof(Startup).GetTypeInfo().Assembly.GetName().Name + "-v1");
});

4. Controllers

对外action 加上请求方式

[HttpPost] [HttpGet]...

访问地址:http://localhost:{端口}/swagger/index.html

上一篇:CrawlSpider模板


下一篇:uni-app调用原生的文件系统管理器(可选取附件上传)