每种方法的 Swashbuckle 自定义架构

Swashbuckle custom schema for each method

我正在使用 API 方法,它需要 class 的对象,如下所示:

public class MyObject
{
    public int ID { get; set; }
    public string Name { get; set; }
}

现在我有两个 API 调用 > InsertUpdate

Insert 方法仅需要来自该对象的 Name,但 Update 还需要 ID

我想要实现的是以某种方式隐藏 ID 从模式中显示在 swagger 中的函数 Insert 下,但显示在 ID.

我说的架构是里面的图片:

我可以通过添加 [JsonIgnore] 从任何地方隐藏 ID 但这并没有解决我的问题,因为我想在 Update

中显示该字段

根据最佳实践 (separation of concerns),我将封装您的数据库逻辑并将您的模型公开为 2 个单独的 DTO,用于插入和更新操作。

类似于:

MyObjectCreateDto.cs

public class MyObjectCreateDto
{
    public string Name { get; set; }
}

MyObjectUpdateDto.cs

public class MyObjectUpdateDto
{
    public int ID { get; set; }
    public string Name { get; set; }
}

然后您可以将上述模型用于您的端点,Swagger 将采用这些模型。