gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:25:37 26 4
gpt4 key购买 nike

我将 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com