1.通过NuGet引入 Swashbuckle.AspNetCore
2.将自定义服务通过依赖注入的方式添加到 IServiceCollection 容器中
其中 WeatherForecast 和 Test 是Controller控制器
SwaggerDoc中的第一个参数 name,此处的值参考控制器接口名上方添加
[ApiExplorerSettings(GroupName = “WeatherForecast”)]
[ApiExplorerSettings(GroupName = “Test”)]
2.容器中的服务,最终在 Configure 方法中进行使用
3.运行程序,在域名后面输入/swagger
通过下图可以清楚看到之前代码中各个属性在页面中是如何展示的
4.让某个接口不在swagger文档中出现
在接口名上方添加
[HttpGet]
[ApiExplorerSettings(IgnoreApi = true)] //让此接口不在swagger文档中出现
public ActionResult<string> Cs2()
{
return "Cs2";
}
5.出现下面报错
我的问题是没有指定接口的请求方式,缺少[HttpGet]或[HttpPost]等
[HttpGet] //指定接口的请求方式
[ApiExplorerSettings(IgnoreApi = true)] //让此接口不在swagger文档中出现
public ActionResult<string> Cs2()
{
return "Cs2";
}