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

标签 c# asp.net-core swagger asp.net-core-webapi swashbuckle

我将 Swashbuckle 6 (Swagger) 与 ASP.NET Core Web API 结合使用。我的模型以 DTO 作为后缀,例如,

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

如何在生成的文档中将其重命名为“测试”?我试过添加带有名称的 DataContract 属性,但这没有帮助。

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

最佳答案

想通了......类似于这里的答案: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;
}

关于c# - 使用 ASP.NET Core Web API 在 Swashbuckle 6 (Swagger) 中重命名模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40644052/

相关文章:

c# - LINQ - 将字符串转换为日期时间

c# - 无法在 .NET Core 2.0 的启动构造函数中注入(inject)我自己的对象

attributes - 通过属性版本控制在 Swagger 中利用 MultipleApiVersions

javascript - '不支持 ES 模块的 require()。 ' Node.js、express、swagger-jsdoc 出错

c# - 创建没有配置文件的 WCF 客户端

c# - btnClose 设置为 CausesValidation = false 但不起作用

c# - mvc-model 日期范围注释动态日期

azure - 捕获所有异常

c# - 使用 NUnit 对 DateTime Controller 进行单元测试

c# - 为什么 Swashbuckle.aspnet.core.swagger 不被识别