【问题标题】:How to add method description in Swagger UI when using minimal APIs using dotnet 6.0?使用 dotnet 6.0 使用最小 API 时如何在 Swagger UI 中添加方法描述?
【发布时间】:2021-12-03 21:30:57
【问题描述】:

This link 有关于如何将文档添加到 swagger UI 的精彩文档。

该链接的问题在于它没有显示如何使用最少的 API 来做到这一点。互联网上有很多地方展示了如何向 swagger UI 添加文档,例如 this other question from stack overflow,但我无法使用最少的 api 来做到这一点。

  1. This video 展示了如何使用最少的 api 创建一个简单的项目:

  2. This video 展示了如何使其可单元测试

  3. This last video 显示如何添加验证、授权和身份验证

是否有人设法使用最少的 API 将自定义文档添加到 Swagger UI 中?

【问题讨论】:

    标签: .net-core swagger openapi


    【解决方案1】:

    根据 Microsoft 文档,支持最小 api。

    app.MapGet("/api/todoitems/{id}", async (int id, TodoDb db) =>
             await db.Todos.FindAsync(id) 
             is Todo todo
             ? Results.Ok(todo) 
             : Results.NotFound())
       .Produces<Todo>(StatusCodes.Status200OK)
       .Produces(StatusCodes.Status404NotFound);

    您可以在这里找到更多信息:

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-6.0#openapi

    【讨论】:

      猜你喜欢
      • 2019-03-23
      • 2022-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 2022-10-25
      • 2020-11-21
      相关资源
      最近更新 更多