Swagger又被成为openapi,是用来生成API的帮助文档的,废话不多说,直接说配置
- 安装
install-package swashbuckle.AspNetCore
- 配置Startup.cs
在ConfigureServices里添加如下code
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo()
{
Title = "API",
Version = "V1"
});
});
在configure里添加如下code
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1");
});
-
运行程序
访问https://localhost:44344/swagger/v1/swagger.json 看的是json
访问https://localhost:44344/swagger 看到的是UI -
假如某个controller的action不想被展示,则通过设置ApiExplorerSettings特性,将其IgnoreApi属性设置为true
[HttpGet("{id}")]
[ApiExplorerSettings(IgnoreApi =true)]
public Author Details(Guid id)
{
return _dataAccessProvider.GetPatientSingleRecord(id);
}