首页 文章

使用ASP.NET Core Web API在Swashbuckle 6(Swagger)中重命名模型

提问于
浏览
1

我正在使用Swashbuckle 6(Swagger)和ASP.NET Core Web API . 我的模型将DTO作为后缀,例如,

public class TestDTO {
    public int Code { get; set; }
    public string Message { get; set; }
}

如何在生成的文档中将其重命名为“Test”?我尝试使用名称添加DataContract属性,但这没有用 .

[HttpGet]
public IActionResult Get() {
  //... create List<TestDTO>
  return Ok(list);
}

1 回答

  • 4

    想出来......类似于这里的答案:Swashbuckle rename Data Type in Model

    唯一的区别是该属性现在称为CustomSchemaIds而不是SchemaId:

    options.CustomSchemaIds(schemaIdStrategy);
    

    而不是查看DataContract属性,我只是删除“DTO”:

    private static string schemaIdStrategy(Type currentClass) {
        string returnedValue = currentClass.Name;
        if (returnedValue.EndsWith("DTO"))
            returnedValue = returnedValue.Replace("DTO", string.Empty);
        return returnedValue;
    }
    

相关问题